Repository navigation
Expand file tree
/
Copy pathSearch.cpp
More file actions
736 lines (662 loc) · 15.3 KB
/
Copy pathSearch.cpp
File metadata and controls
736 lines (662 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "kilib/stdafx.h"
#include "rsrc/resource.h"
#include "Search.h"
#include "NSearch.h"
#include "RSearch.h"
using namespace ki;
using namespace editwing;
using view::VPos;
//-------------------------------------------------------------------------
// Replacement escape decoding (regexp mode only) and multiline end calc
//-------------------------------------------------------------------------
static ulong DecodeReplacement( const wchar_t* src, wchar_t* dst )
{
ulong di = 0;
for( ulong si=0; src[si]!=L'\0'; ++si )
{
if( src[si]==L'\\' && src[si+1]!=L'\0' )
{
wchar_t n = src[++si];
switch( n )
{
case L't': dst[di++]=L'\t'; break;
case L'n': dst[di++]=L'\n'; break;
case L'r': dst[di++]=L'\r'; break;
case L'f': dst[di++]=L'\f'; break;
case L'v': dst[di++]=L'\v'; break;
case L'a': dst[di++]=L'\a'; break;
case L'x': case L'X': {
// ASCII + Iso 8859-1 codepoint \xXX
uint v = 0;
for( size_t i = 0; i < 2 && src[si+1] ; i++ )
{
wchar_t ch = src[++si];
if( '0'<=ch && ch<='9' ) v = 16*v + ch-'0';
if( 'A'<=ch && ch<='F' ) v = 16*v + ch-'A'+10;
if( 'a'<=ch && ch<='f' ) v = 16*v + ch-'a'+10;
}
dst[di++] = (wchar_t)v;
}break;
case L'u': {
// UCS2 codepoint \uXXXX
uint v = 0;
for( size_t i = 0; i < 4 && src[si+1] ; i++ )
{
wchar_t ch = src[++si];
if( '0'<=ch && ch<='9' ) v = 16*v + ch-'0';
if( 'A'<=ch && ch<='F' ) v = 16*v + ch-'A'+10;
if( 'a'<=ch && ch<='f' ) v = 16*v + ch-'a'+10;
}
dst[di++] = (wchar_t)v;
}break;
case L'U': {
// UTF-32 codepoint \UXXXXXXXX
uint v = 0;
for( size_t i = 0; i < 8 && src[si+1] ; i++ )
{
wchar_t ch = src[++si];
if( '0'<=ch && ch<='9' ) v = 16*v + ch-'0';
if( 'A'<=ch && ch<='F' ) v = 16*v + ch-'A'+10;
if( 'a'<=ch && ch<='f' ) v = 16*v + ch-'a'+10;
}
if( v < 0x10000 )
{
dst[di++] = (wchar_t)v;
}
else
{
// Surrogate pair...
dst[di++] = (wchar_t)(0xD800 + (((v-0x10000)>>10)&0x3ff)),
dst[di++] = (wchar_t)(0xDC00 + (((v-0x10000) )&0x3ff));
}
}break;
default: dst[di++]=n; break;
}
}
else
{
dst[di++] = src[si];
}
}
dst[di] = L'\0';
return di;
}
static DPos ReplaceEndPos( const DPos& b, const wchar_t* ustr, ulong ulen )
{
ulong breaks = 0;
ulong lastBreakEnd = 0;
for( ulong i=0; i<ulen; )
{
if( ustr[i]==L'\r' )
{
++breaks;
++i;
if( i<ulen && ustr[i]==L'\n' )
++i;
lastBreakEnd = i;
}
else if( ustr[i]==L'\n' )
{
++breaks;
++i;
lastBreakEnd = i;
}
else
{
++i;
}
}
DPos e;
if( breaks == 0 )
{
e.tl = b.tl;
e.ad = b.ad + ulen;
}
else
{
e.tl = b.tl + breaks;
e.ad = ulen - lastBreakEnd;
}
return e;
}
//-------------------------------------------------------------------------
SearchManager::SearchManager( ki::Window& w, editwing::EwEdit& e )
: DlgImpl( IDD_FINDREPLACE )
, edit_( e )
, searcher_( NULL )
, mainWnd_( w )
, bIgnoreCase_( true ) // 1.08 default true
, bRegExp_( false )
, bDownSearch_( true )
, bChanged_ (false )
, inichanged_( false )
{
}
SearchManager::~SearchManager()
{
if(searcher_)
delete searcher_;
}
void SearchManager::SaveToINI()
{
if (inichanged_)
{
ki::IniFile ini;
inichanged_ = false;
ini.SetSectionAsUserName();
ini.PutBool( TEXT("SearchIgnoreCase"), bIgnoreCase_ );
ini.PutBool( TEXT("SearchRegExp"), bRegExp_ );
}
}
void SearchManager::LoadFromINI()
{
ki::IniFile ini;
inichanged_ = false;
ini.SetSectionAsUserName();
bIgnoreCase_ = ini.GetBool( TEXT("SearchIgnoreCase"), bIgnoreCase_ );
bRegExp_ = ini.GetBool( TEXT("SearchRegExp"), bRegExp_ );
}
//-------------------------------------------------------------------------
// ダイアログ関係
//-------------------------------------------------------------------------
void SearchManager::ShowDlg()
{
// GoModal( ::GetParent(edit_.hwnd()) );
if( isAlive() )
{
SetActive();
}
else
{
GoModeless( ::GetParent(edit_.hwnd()) );
SetCenter(hwnd(), edit_.hwnd());
ShowUp();
}
}
bool SearchManager::TrapMsg(MSG* msg)
{
if( ! isAlive() || type()==MODAL )
return false;
return DlgImpl::PreTranslateMessage(msg);
}
void SearchManager::on_init()
{
if( bIgnoreCase_ ) CheckItem( IDC_IGNORECASE );
if( bRegExp_ ) CheckItem( IDC_REGEXP );
const VPos *stt, *end;
edit_.getCursor().getCurPos( &stt, &end );
// Set non multiline selection as find string.
if( edit_.getCursor().isSelected() && stt->tl == end->tl )
{
// 選択されている状態では、基本的にそれをボックスに表示
ulong dmy;
aarr<unicode> str = edit_.getCursor().getSelectedStr();
ulong len=0;
for( ; str[len]!=L'\0' && str[len]!=L'\n'; ++len );
str[len] = L'\0';
if( searcher_ &&
searcher_->Search( str.get(), len, 0, &dmy, &dmy ) )
{
SetItemText( IDC_FINDBOX, findStr_.c_str() );
}
else
{
#ifdef _UNICODE
SetItemText( IDC_FINDBOX, str.get() );
#else
char *ab = (char*)TS.alloc( (len+1) * 3 * sizeof(TCHAR) );
if( ab )
{
::WideCharToMultiByte( CP_ACP, 0, str.get(), -1,
ab, (len+1)*3, NULL, NULL );
SetItemText( IDC_FINDBOX, ab );
TS.freelast( ab, (len+1) * 3 * sizeof(TCHAR) );
}
#endif
}
}
else
{
SetItemText( IDC_FINDBOX, findStr_.c_str() );
}
SetItemText( IDC_REPLACEBOX, replStr_.c_str() );
fillFromHistoric(IDC_FINDBOX, findHistoric_, countof(findHistoric_) );
fillFromHistoric(IDC_REPLACEBOX, replHistoric_, countof(replHistoric_) );
::SetFocus( item(IDC_FINDBOX) );
SendMsgToItem( IDC_FINDBOX, EM_SETSEL, 0, ::GetWindowTextLength(item(IDC_FINDBOX)) );
bChanged_ = true;
}
void SearchManager::on_destroy()
{
bChanged_ = false;
}
bool SearchManager::on_command( UINT cmd, UINT id, HWND ctrl )
{
if( cmd==CBN_SELCHANGE || cmd == CBN_EDITCHANGE )
{
// 文字列変更があったことを記憶
bChanged_ = true;
}
else if( cmd==BN_CLICKED )
{
switch( id )
{
// チェックボックスの変更があったことを記憶
case IDC_IGNORECASE:
case IDC_REGEXP:
bChanged_ = true;
break;
// ボタンが押された場合
case ID_FINDNEXT:
on_findnext();
break;
case ID_FINDPREV:
on_findprev();
break;
case ID_REPLACENEXT:
on_replacenext();
break;
case ID_REPLACEALL:
on_replaceall();
break;
}
}
else
{
return false;
}
return true;
}
bool SearchManager::on_cancel()
{
UpdateData();
saveToHistoric(IDC_FINDBOX, findHistoric_, countof(findHistoric_) );
saveToHistoric(IDC_REPLACEBOX, replHistoric_, countof(replHistoric_) );
return true;
}
void SearchManager::on_findnext()
{
UpdateData();
ConstructSearcher();
if( isReady() )
{
FindNextImpl();
// End( IDOK );
}
}
void SearchManager::on_findprev()
{
UpdateData();
ConstructSearcher( false );
if( isReady() )
FindPrevImpl();
}
void SearchManager::on_replacenext()
{
UpdateData();
ConstructSearcher();
if( isReady() )
ReplaceImpl();
}
void SearchManager::on_replaceall()
{
UpdateData();
ConstructSearcher();
if( isReady() )
ReplaceAllImpl();
}
void SearchManager::UpdateData()
{
// ダイアログから変更点を取り込み
bool IgnoreCase = isItemChecked( IDC_IGNORECASE );
bool RegExp = isItemChecked( IDC_REGEXP );
// Must we save save to ini?
inichanged_ = bIgnoreCase_ != IgnoreCase || RegExp != bRegExp_;
bIgnoreCase_ = IgnoreCase;
bRegExp_ = RegExp;
SaveToINI();
TCHAR* str;
LRESULT n = SendMsgToItem( IDC_FINDBOX, WM_GETTEXTLENGTH );
str = (TCHAR*)TS.alloc( sizeof(TCHAR) * (n+1) );
if( str )
{
GetItemText( IDC_FINDBOX, n+1, str );
findStr_ = str;
AddToComboBoxHistoric(IDC_FINDBOX, str);
TS.freelast( str, sizeof(TCHAR) * (n+1) );
}
n = SendMsgToItem( IDC_REPLACEBOX, WM_GETTEXTLENGTH );
str = (TCHAR*)TS.alloc( sizeof(TCHAR) * (n+1) );
if( str )
{
GetItemText( IDC_REPLACEBOX, n+1, str );
replStr_ = str;
AddToComboBoxHistoric(IDC_REPLACEBOX, str);
TS.freelast( str, sizeof(TCHAR) * (n+1) );
}
}
void SearchManager::ConstructSearcher( bool down )
{
bChanged_ = (bChanged_ || (bDownSearch_ != down));
if( (bChanged_ || !isReady()) && findStr_.len()!=0 )
{
// 検索者作成
bDownSearch_ = down;
const unicode *u = findStr_.ConvToWChar();
if( searcher_ )
delete searcher_;
searcher_ = NULL;
if( bRegExp_ )
searcher_ = new RSearch( u, !bIgnoreCase_, bDownSearch_ );
else
if( bDownSearch_ )
if( bIgnoreCase_ )
searcher_ = new NSearch<IgnoreCase>(u);
else
searcher_ = new NSearch<CaseSensitive>(u);
else
if( bIgnoreCase_ )
searcher_ = new NSearchRev<IgnoreCase>(u);
else
searcher_ = new NSearchRev<CaseSensitive>(u);
findStr_.FreeWCMem(u);
// 変更終了フラグ
bChanged_ = false;
}
}
//-------------------------------------------------------------------------
void SearchManager::FindNext()
{
if( !isReady() )
{
ShowDlg();
}
else
{
ConstructSearcher();
if( isReady() )
FindNextImpl();
}
}
void SearchManager::FindPrev()
{
if( !isReady() )
{
ShowDlg();
}
else
{
ConstructSearcher( false );
if( isReady() )
FindPrevImpl();
}
}
//-------------------------------------------------------------------------
// 実際の処理の実装
//-------------------------------------------------------------------------
void SearchManager::FindNextImpl(bool redo)
{
// カーソル位置取得
const VPos *stt, *end;
edit_.getCursor().getCurPos( &stt, &end );
// 選択範囲ありなら、選択範囲先頭の1文字先から検索
// そうでなければカーソル位置から検索
DPos s = *stt;
if( *stt != *end )
{
if( stt->ad == edit_.getDoc().len(stt->tl) )
s = DPos( stt->tl+1, 0 );
else
s = DPos( stt->tl, stt->ad+1 );
}
// 検索
DPos b, e;
if( FindNextFromImpl( s, &b, &e ) )
{
// 見つかったら選択
edit_.getCursor().MoveCur( b, false );
edit_.getCursor().MoveCur( e, true );
return;
}
// 見つからなかった場合
NotFound(!redo);
}
void SearchManager::NotFound(bool GoingDown)
{
//MsgBox( RzsString(IDS_NOTFOUND).c_str() );
if (GoingDown) {
if (IDOK == MsgBox( RzsString(IDS_NOTFOUNDDOWN).c_str(), NULL, MB_OKCANCEL )) {
edit_.getCursor().MoveCur( DPos(0,0), false );
FindNextImpl(true);
}
} else {
MsgBox(RzsString(IDS_NOTFOUND).c_str(), NULL, MB_OK);
}
}
void SearchManager::FindPrevImpl()
{
// カーソル位置取得
const VPos *stt, *end;
edit_.getCursor().getCurPos( &stt, &end );
if( stt->ad!=0 || stt->tl!=0 )
{
// 選択範囲先頭の1文字前から検索
DPos s;
if( stt->ad == 0 )
s = DPos( stt->tl-1, edit_.getDoc().len(stt->tl-1) );
else
s = DPos( stt->tl, stt->ad-1 );
// 検索
DPos b, e;
if( FindPrevFromImpl( s, &b, &e ) )
{
// 見つかったら選択
edit_.getCursor().MoveCur( b, false );
edit_.getCursor().MoveCur( e, true );
return;
}
}
// 見つからなかった場合
NotFound();
}
bool SearchManager::FindNextFromImpl( DPos s, DPos* beg, DPos* end )
{
// 1行ずつサー, Search one line at a time
const doc::Document& d = edit_.getDoc();
for( ulong mbg,med,e=d.tln(); s.tl<e; ++s.tl, s.ad=0 )
if( searcher_ && searcher_->Search(
d.tl(s.tl), d.len(s.tl), s.ad, &mbg, &med ) )
{
beg->tl = end->tl = s.tl;
beg->ad = mbg;
end->ad = med;
return true; // 発見, Found!
}
return false;
}
bool SearchManager::FindPrevFromImpl( DPos s, DPos* beg, DPos* end )
{
// 1行ずつサーチ, Search one line at a time
const doc::Document& d = edit_.getDoc();
for( ulong mbg,med; ; s.ad=d.len(--s.tl) )
{
if( searcher_ && searcher_->Search(
d.tl(s.tl), d.len(s.tl), s.ad, &mbg, &med ) )
{
beg->tl = end->tl = s.tl;
beg->ad = mbg;
end->ad = med;
return true; // 発見, Found!
}
if( s.tl==0 )
break;
}
return false;
}
void SearchManager::ReplaceImpl()
{
// カーソル位置取得
const VPos *stt, *end;
edit_.getCursor().getCurPos( &stt, &end );
// 選択範囲先頭から検索
DPos b, e;
if( FindNextFromImpl( *stt, &b, &e ) )
{
if( e == *end )
{
const wchar_t* raw = replStr_.ConvToWChar();
const ulong rawLen = my_lstrlenW( raw );
unicode* dec = NULL;
const unicode* ustr = raw;
ulong ulen = rawLen;
if( bRegExp_ )
{
dec = (unicode *)TS.alloc( (rawLen + 1) * sizeof(unicode) );
if (!dec) { replStr_.FreeWCMem( raw ); return; }
ulen = DecodeReplacement( raw, dec );
ustr = dec;
}
// 置換
edit_.getDoc().Execute( doc::Replace( b, e, ustr, ulen ) );
DPos nxt = ReplaceEndPos( b, ustr, ulen );
replStr_.FreeWCMem( raw );
if( dec ) TS.freelast( dec, (rawLen + 1) * sizeof(unicode) );
if( FindNextFromImpl( nxt, &b, &e ) )
{
// 次を選択
edit_.getCursor().MoveCur( b, false );
edit_.getCursor().MoveCur( e, true );
return;
}
}
else
{
// そうでなければとりあえず選択
edit_.getCursor().MoveCur( b, false );
edit_.getCursor().MoveCur( e, true );
return;
}
}
// 見つからなかった場合
NotFound();
}
void SearchManager::ReplaceAllImpl()
{
// まず、実行する置換を全てここに登録する
doc::MacroCommand mcr;
// 置換後文字列
const wchar_t* raw = replStr_.ConvToWChar();
const ulong rawLen = my_lstrlenW( raw );
unicode* dec = NULL;
const unicode* ustr = raw;
ulong ulen = rawLen;
if( bRegExp_ )
{
dec = (unicode *)TS.alloc( (rawLen + 1) * sizeof(unicode) );
if (!dec) { replStr_.FreeWCMem( raw ); return; }
ulen = DecodeReplacement( raw, dec );
ustr = dec;
}
// Get selection position
const VPos *stt, *end;
edit_.getCursor().getCurPos( &stt, &end );
DPos oristt = *stt;
// Set begining and end for replace all
// if multi-line selection
DPos s(0,0), dend(0, 0);
bool noselection = true;
if(stt->tl != end->tl)
{ // Multi-line selection
noselection = false;
s = *stt; // Start of selection
dend = *end; // End of selection
}
// 文書の頭から検索, Search from the beginning of the document (or selection)
ulong tlAdd = 0;
long adDif = 0;
ulong prevTl = 0;
bool firstHit = true;
DPos b, e;
while( FindNextFromImpl( s, &b, &e ) && (noselection || e <= dend) )
{ // search until the end of selection if any
if( b.tl == e.tl && b.ad == e.ad )
{
if( s.ad < edit_.getDoc().len( s.tl ) )
s.ad = s.ad + 1;
else
s = DPos( s.tl+1, 0 );
if( s.tl >= edit_.getDoc().tln() )
break;
continue;
}
if( firstHit )
{
prevTl = b.tl;
firstHit = false;
}
else if( b.tl != prevTl )
{
adDif = 0;
prevTl = b.tl;
}
s = e;
ulong obAd = b.ad;
ulong oeAd = e.ad;
b.tl += tlAdd; b.ad += adDif;
e.tl += tlAdd; e.ad += adDif;
// 置換コマンドを登録
mcr.Add( new doc::Replace(b,e,ustr,ulen) );
ulong newBreaks = 0;
ulong lastSeg = ulen;
for( ulong ri=0; ri<ulen; )
{
if( ustr[ri]==L'\r' )
{
++newBreaks;
++ri;
if( ri<ulen && ustr[ri]==L'\n' )
++ri;
lastSeg = ulen - ri;
}
else if( ustr[ri]==L'\n' )
{
++newBreaks;
++ri;
lastSeg = ulen - ri;
}
else
{
++ri;
}
}
if( newBreaks == 0 )
adDif += (long)ulen - (long)(oeAd - obAd);
else
adDif = (long)lastSeg - (long)oeAd;
tlAdd += newBreaks;
}
if( mcr.size() > 0 )
{
// ここで連続置換
edit_.getDoc().Execute( mcr );
// カーソル移動
DPos lastEnd = ReplaceEndPos( b, ustr, ulen );
edit_.getCursor().MoveCur( lastEnd, false );
if (noselection)
{
edit_.getCursor().MoveCur( e, false );
}
else
{ // Re-select the text that was modified if needed.
edit_.getCursor().MoveCur( oristt, false );
edit_.getCursor().MoveCur( DPos(dend.tl, dend.ad+adDif), true );
}
// 閉じる?
End( IDOK );
}
TCHAR str[256+INT_DIGITS+1];
::wsprintf( str, RzsString(IDS_REPLACEALLDONE).c_str(), mcr.size() );
MsgBox( str, RzsString(IDS_APPNAME).c_str(), MB_ICONINFORMATION );
if( dec ) TS.freelast( dec, (rawLen + 1) * sizeof(unicode) );
replStr_.FreeWCMem( raw );
}