-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.cpp
533 lines (469 loc) · 10.7 KB
/
Search.cpp
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
#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;
//-------------------------------------------------------------------------
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() );
::SetFocus( item(IDC_FINDBOX) );
SendMsgToItem( IDC_FINDBOX, EM_SETSEL, 0,
::GetWindowTextLength(item(IDC_FINDBOX)) );
}
void SearchManager::on_destroy()
{
bChanged_ = false;
}
bool SearchManager::on_command( UINT cmd, UINT id, HWND ctrl )
{
if( cmd==EN_CHANGE )
{
// 文字列変更があったことを記憶
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();
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;
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;
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* ustr = replStr_.ConvToWChar();
const ulong ulen = my_lstrlenW( ustr );
// 置換
edit_.getDoc().Execute( doc::Replace(
b, e, ustr, ulen
) );
replStr_.FreeWCMem( ustr );
if( FindNextFromImpl( DPos(b.tl,b.ad+ulen), &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* ustr = replStr_.ConvToWChar();
const ulong ulen = my_lstrlenW( ustr );
// 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)
int dif=0;
DPos b, e;
while( FindNextFromImpl( s, &b, &e ) && (noselection || e <= dend) )
{ // search until the end of selection if any
if( s.tl != b.tl ) dif = 0;
s = e;
// 置換コマンドを登録
b.ad += dif, e.ad += dif;
mcr.Add( new doc::Replace(b,e,ustr,ulen) );
dif -= e.ad-b.ad-ulen;
}
if( mcr.size() > 0 )
{
// ここで連続置換
edit_.getDoc().Execute( mcr );
// カーソル移動
e.ad = b.ad + ulen;
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+dif), 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 );
replStr_.FreeWCMem( ustr );
}