File Coverage

lib/Kephra/Edit/Search.pm
Criterion Covered Total %
statement 6 419 1.4
branch 0 200 0.0
condition 0 130 0.0
subroutine 2 45 4.4
pod 0 31 0.0
total 8 825 0.9


line stmt bran cond sub pod time code
1             package Kephra::Edit::Search;
2             our $VERSION = '0.31';
3            
4 1     1   1137 use strict;
  1         2  
  1         34  
5 1     1   7 use warnings;
  1         2  
  1         5604  
6            
7             # internal and menu functions about find and replace text
8             # drag n drop target class
9            
10             # internal functions
11             my ($find_item, $found_pos, $old_pos, $replace_item);
12             my $flags;
13             my $history_refresh;
14             my @find_history;
15             my @replace_history;
16 0     0     sub _config { Kephra::API::settings()->{search} }
17 0     0     sub _attributes{ _config()->{attribute} }
18 0     0     sub _history { _config()->{history} }
19 0     0     sub _find_pos { $found_pos }
20            
21             sub _refresh_search_flags {
22 0     0     my $attr = _attributes();
23 0           $flags = 0;
24            
25 0 0 0       $flags |= &Wx::wxSTC_FIND_MATCHCASE
26             if defined $attr->{match_case} and $attr->{match_case};
27 0 0 0       if ( defined $attr->{match_whole_word} and $attr->{match_whole_word} ) {
28 0           $flags |= &Wx::wxSTC_FIND_WHOLEWORD
29             } else {
30 0 0 0       $flags |= &Wx::wxSTC_FIND_WORDSTART
31             if $attr->{match_word_begin} and $attr->{match_word_begin};
32             }
33 0 0 0       $flags |= &Wx::wxSTC_FIND_REGEXP
34             if defined $attr->{match_regex} and $attr->{match_regex};
35             }
36            
37            
38             sub load_search_data {
39 0     0 0   my $file = Kephra::Config::filepath( _config()->{data_file} );
40 0           my $config_tree = Kephra::Config::File::load($file);
41            
42 0   0       set_find_item( $config_tree->{find}{item} || '' );
43 0   0       set_replace_item( $config_tree->{replace}{item} || '' );
44 0 0         if (defined $config_tree->{find}{history}){
45 0 0         if (ref $config_tree->{find}{history} eq 'ARRAY'){
46 0           @find_history = @{ $config_tree->{find}{history} };
  0            
47 0           } else { $find_history[0] = $config_tree->{find}{history} }
48             }
49 0 0         if (defined $config_tree->{replace}{history}){
50 0 0         if (ref $config_tree->{replace}{history} eq 'ARRAY'){
51 0           @replace_history = @{ $config_tree->{replace}{history} };
  0            
52 0           } else { $replace_history[0] = $config_tree->{replace}{history} }
53             }
54 0           my $history = _history();
55            
56             # remove dups and cut to the configured length
57 0 0         if ( $history->{use} ) {
58 0           my ( %seen1, %seen2 );
59 0           my @uniq = grep { !$seen1{$_}++ } @find_history;
  0            
60 0           @find_history = splice @uniq, 0, $history->{length};
61 0           @uniq = grep { !$seen2{$_}++ } @replace_history;
  0            
62 0           @replace_history = splice @uniq, 0, $history->{length};
63             } else {
64 0           @find_history = ();
65 0           @replace_history = ();
66             }
67             # search item is findable
68 0           $found_pos = 0;
69 0           Kephra::EventTable::trigger
70             ('find.item.history.changed', 'replace.item.history.changed');
71 0           Kephra::Edit::Marker::restore_bookmarks( $config_tree->{bookmark} );
72             }
73            
74             sub save_search_data {
75 0     0 0   my $file = Kephra::Config::filepath( _config()->{data_file} );
76 0           my $config_tree = Kephra::Config::File::load($file);
77 0           $config_tree->{find}{item} = get_find_item();
78 0           $config_tree->{find}{history} = get_find_history();
79 0           $config_tree->{replace}{item} = get_replace_item();
80 0           $config_tree->{replace}{history} = get_replace_history();
81 0           $config_tree->{bookmark} = Kephra::Edit::Marker::get_bookmark_data();
82 0           Kephra::Config::File::store($file, $config_tree);
83 0           Kephra::Edit::Marker::store();
84             }
85             #
86 0 0   0 0   sub get_find_item { $find_item || ''}
87             sub set_find_item {
88 0     0 0   my $old = $find_item;
89 0           my $new = shift;
90 0 0 0       if (defined $new and (not defined $old or $new ne $old)){
      0        
91 0           $find_item = $new;
92 0           $found_pos = -1;
93 0           Kephra::EventTable::trigger('find.item.changed');
94             }
95             }
96            
97             sub set_selection_as_find_item {
98 0     0 0   set_find_item( Kephra::App::EditPanel::_ref()->GetSelectedText )
99             }
100            
101 0     0 0   sub item_findable { _exist_find_item() }
102 0 0 0 0     sub _exist_find_item { (defined $find_item and $find_item) ? 1 : 0 }
103 0 0 0 0     sub _exist_replace_item { (defined $replace_item and $replace_item) ? 1 : 0 }
104            
105 0 0   0 0   sub get_replace_item { $replace_item || ''}
106             sub set_replace_item {
107 0     0 0   my $old = $replace_item;
108 0           my $new = shift;
109 0 0 0       if (defined $new and (not defined $old or $new ne $old)){
      0        
110 0           $replace_item = $new;
111 0           Kephra::EventTable::trigger('replace.item.changed');
112             }
113             }
114            
115             sub set_selection_as_replace_item {
116 0     0 0   set_replace_item( Kephra::App::EditPanel::_ref()->GetSelectedText )
117             }
118 0     0 0   sub get_find_history { \@find_history }
119 0     0 0   sub get_replace_history { \@replace_history }
120             sub refresh_find_history {
121 0     0 0   my $found_match = shift;
122 0           my $find_item = get_find_item();
123 0           my $history = _history();
124 0 0         return unless $history->{use};
125             # check if refresh needed
126 0 0 0       return if $history->{remember_only_matched} and not $found_match;
127            
128 0 0 0       if ($find_item and $find_history[0] ne $find_item) {
129 0           for ( 0 .. $#find_history ) { # delete the dup
130 0 0         if ( $find_history[$_] eq $find_item ) {
131 0           @find_history = @find_history[ 0 .. $_-1, $_+1 .. $#find_history ];
132 0           last;
133             }
134 0 0         pop @find_history if $_ == $history->{length} - 1;
135             }
136 0           unshift @find_history, $find_item; # insert new history item
137 0           Kephra::EventTable::trigger('find.item.history.changed');
138             }
139             }
140             sub refresh_replace_history {
141 0     0 0   my $replace_item = get_replace_item();
142 0           my $history = _history();
143            
144 0 0         if ($replace_item) {
145 0           for ( 0 .. $#replace_history ) {
146 0 0         if ( $replace_history[$_] eq $replace_item ) {
147 0           @replace_history = @replace_history[ 0 .. $_-1, $_+1 .. $#replace_history ];
148 0           last;
149             }
150 0 0         pop @replace_history if $_ == $history->{length} - 1;
151             }
152 0           unshift @replace_history, $replace_item;
153 0           Kephra::EventTable::trigger('replace.item.history.changed');
154             }
155             }
156            
157             sub _caret_2_sel_end {
158 0     0     my $ep = Kephra::App::EditPanel::_ref();
159 0           my $pos = $ep->GetCurrentPos;
160 0           my $sel_start = $ep->GetSelectionStart;
161 0           my $sel_end = $ep->GetSelectionEnd;
162 0 0         if ( $pos != $sel_end ) {
163 0           $ep->SetCurrentPos($sel_end);
164 0           $ep->SetSelectionStart($sel_start);
165             }
166             }
167            
168             #
169 0     0 0   sub set_range{ _attributes()->{in} = shift }
170 0     0 0   sub get_range{ _attributes()->{in} }
171            
172             sub get_attribute{
173 0     0 0   my $attr = shift;
174 0 0 0       if ($attr eq 'match_case' or
      0        
      0        
      0        
      0        
175             $attr eq 'match_word_begin'or
176             $attr eq 'match_whole_word'or
177             $attr eq 'match_regex' or
178             $attr eq 'auto_wrap' or
179             $attr eq 'incremental' ) {
180 0           _attributes()->{$attr}
181             }
182             }
183            
184             sub switch_attribute{
185 0     0 0   my $attr = shift;
186 0 0 0       if ($attr eq 'match_case' or
      0        
      0        
      0        
      0        
187             $attr eq 'match_word_begin'or
188             $attr eq 'match_whole_word'or
189             $attr eq 'match_regex' or
190             $attr eq 'auto_wrap' or
191             $attr eq 'incremental' ) {
192 0 0         unless (defined _attributes->{$attr})
193 0           { _attributes->{$attr} = 1 }
194 0           else { _attributes->{$attr} ^= 1 }
195 0 0         _refresh_search_flags() if substr($attr, 0, 1) eq 'm';
196             }
197             }
198            
199             # find helper function
200             sub replace_selection {
201 0     0 0   Kephra::App::EditPanel::_ref()->ReplaceSelection( get_replace_item() )
202             }
203            
204             sub _find_next {
205 0     0     my $ep = Kephra::App::EditPanel::_ref();
206 0           $ep->SearchAnchor;
207 0           $found_pos = $ep->SearchNext( $flags, get_find_item() );
208 0           Kephra::EventTable::trigger('find');
209 0           return $found_pos;
210             }
211            
212             sub _find_prev {
213 0     0     my $ep = Kephra::App::EditPanel::_ref();
214 0           $ep->SearchAnchor;
215 0           $found_pos = $ep->SearchPrev( $flags, get_find_item() );
216 0           Kephra::EventTable::trigger('find');
217 0           return $found_pos;
218             }
219            
220             sub _find_first {
221 0     0     Kephra::Edit::Goto::_pos(0);
222 0           _find_next();
223             }
224            
225             sub _find_last {
226 0     0     Kephra::Edit::Goto::_pos(-1);
227 0           _find_prev();
228             }
229            
230            
231             sub first_increment {
232 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
233 0 0         if ( _exist_find_item() ) {
234 0           Kephra::Edit::_save_positions;
235 0 0         if ( _find_first() > -1 ) {
236             #_caret_2_sel_end();
237 0           Kephra::Edit::_center_caret;
238 0           return 1;
239             }
240             }
241 0 0         Kephra::Edit::Goto::_pos( $old_pos ) if defined $old_pos;
242 0           return 0;
243             }
244            
245             #sub next_increment {}
246             # find related menu calls
247             sub find_all {
248             #Kephra::Dialog::msg_box(&Wx::wxUNICODE(), '');
249 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
250 0 0         if ( _exist_find_item() ) {
251 0           my $search_result = _find_first();
252 0           my ($sel_start, $sel_end);
253             #Kephra::Dialog::msg_box( , '');
254             #$ep->IndicatorSetStyle(0, &Wx::wxSTC_INDIC_TT );
255             #$ep->IndicatorSetForeground(0, &Wx::Colour->new(0xff, 0x00, 0x00));
256 0           $ep->IndicatorSetStyle(1, &Wx::wxSTC_INDIC_TT );
257 0           $ep->IndicatorSetForeground(1, &Wx::Colour->new(0xff, 0x00, 0x00));
258             # ^= &Wx::wxSTC_INDIC_STRIKE;
259 0           $ep->SetSelection(0,0);
260 0 0         return 0 if $search_result == -1;
261 0           while ($search_result > -1){
262 0           ($sel_start, $sel_end) = $ep->GetSelection;
263 0           Kephra::Edit::Goto::_pos( $sel_end );
264 0           $ep->StartStyling($sel_start, 224);#224
265 0           $ep->SetStyleBytes($sel_end - $sel_start, 128);
266 0           $search_result = _find_next();
267             }
268 0           Kephra::Edit::Goto::_pos( $sel_end );
269 0           $ep->Colourise( 0, $sel_end);
270 0           return 1;
271             } else {
272 0 0         Kephra::Edit::Goto::_pos( $old_pos ) if defined $old_pos;
273 0           return 1;
274             }
275             }
276            
277             sub find_prev {
278 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
279 0           my $attr = _attributes();
280 0           my $return = -1;
281 0 0         if ( _exist_find_item() ) {
282 0           Kephra::Edit::_save_positions;
283 0           Kephra::Edit::Goto::_pos( $ep->GetSelectionStart - 1 );
284 0           $return = _find_prev();
285 0 0         if ( $return == -1 ) {
286 0 0         if ( get_range() eq 'document' ) {
    0          
287 0 0         $return = _find_last() if $attr->{auto_wrap};
288             } elsif ( get_range() eq 'open_docs' ) {
289 0           $Kephra::temp{dialog}{control} = 1;
290 0           my $begin_doc = Kephra::Document::Data::current_nr();
291 0           while ( $return == -1 ) {
292 0           Kephra::Edit::_restore_positions;
293 0 0 0       last if ( ( Kephra::Document::Data::current_nr() == 0 )
294             and !$attr->{auto_wrap} );
295 0           Kephra::Document::Change::tab_left();
296 0           Kephra::Edit::_save_positions();
297 0           $return = _find_last();
298             last
299 0 0         if ( Kephra::Document::Data::current_nr() == $begin_doc );
300             }
301 0           $Kephra::temp{dialog}{control} = 0;
302             }
303             }
304 0 0         if ( $return == -1 ) { Kephra::Edit::_restore_positions() }
  0            
305 0           else { _caret_2_sel_end(); Kephra::Edit::_center_caret() }
  0            
306 0           refresh_find_history($return);
307             }
308 0           $return;
309             }
310            
311             sub find_next {
312 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
313 0           my $attr = _attributes();
314 0           my $return = -1;
315            
316 0 0         if ( _exist_find_item() ) {
317 0           Kephra::Edit::_save_positions();
318 0           Kephra::Edit::Goto::_pos( $ep->GetSelectionEnd );
319 0           $return = _find_next();
320 0 0         if ( $return == -1 ) {
321 0 0         if ( get_range() eq 'document' ) {
    0          
322 0 0         $return = _find_first() if $attr->{auto_wrap};
323             } elsif ( get_range() eq 'open_docs' ) {
324 0           $Kephra::temp{dialog}{control} = 1;
325 0           my $begin_doc = Kephra::Document::Data::current_nr();
326 0           while ( $return == -1 ) {
327 0           Kephra::Edit::_restore_positions();
328 0 0 0       last if Kephra::Document::Data::current_nr()
329             == Kephra::Document::Data::last_nr()
330             and not $attr->{auto_wrap};
331 0           Kephra::Document::Change::tab_right();
332 0           Kephra::Edit::_save_positions();
333 0           $return = _find_first();
334 0 0         last if ( Kephra::Document::Data::current_nr() == $begin_doc );
335             }
336 0           $Kephra::temp{dialog}{control} = 0;
337             }
338             }
339 0 0         if ( $return == -1 ) { Kephra::Edit::_restore_positions() }
  0            
340 0           else { _caret_2_sel_end(); Kephra::Edit::_center_caret(); }
  0            
341 0           refresh_find_history($return);
342             }
343 0           $return;
344             }
345            
346             sub fast_back {
347 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
348 0           my $attr = _attributes();
349 0           my $return = -1;
350 0 0         if (_exist_find_item()) {
351 0           for ( 1 .. $attr->{fast_steps} ) {
352 0           Kephra::Edit::_save_positions();
353 0           Kephra::Edit::Goto::_pos( $ep->GetSelectionStart - 1 );
354 0           $return = _find_prev();
355 0 0         if ( $return == -1 ) {
356 0 0         if ( get_range() eq 'document' ) {
    0          
357 0 0         $return = _find_last() if $attr->{auto_wrap};
358             } elsif ( get_range() eq 'open_docs' ) {
359 0           $Kephra::temp{dialog}{control} = 1;
360 0           my $begin_doc = Kephra::Document::Data::current_nr();
361 0           while ( $return == -1 ) {
362 0           Kephra::Edit::_restore_positions();
363 0 0 0       last if Kephra::Document::Data::current_nr() == 0
364             and not $attr->{auto_wrap};
365 0           Kephra::Document::Change::tab_left();
366 0           Kephra::Edit::_save_positions();
367 0           $return = _find_last();
368 0 0         last if Kephra::Document::Data::current_nr() == $begin_doc;
369             }
370 0           $Kephra::temp{dialog}{control} = 0;
371             }
372             }
373 0 0         refresh_find_history($return) if ( $_ == 1 );
374 0 0         if ( $return == -1 ) { Kephra::Edit::_restore_positions(); last; }
  0            
  0            
375 0           else { _caret_2_sel_end(); Kephra::Edit::_center_caret(); }
  0            
376             }
377             }
378             }
379            
380             sub fast_fore {
381 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
382 0           my $attr = _attributes();
383 0           my $return = -1;
384 0 0         if (_exist_find_item()) {
385 0           for ( 1 .. $attr->{fast_steps} ) {
386 0           Kephra::Edit::_save_positions();
387 0           Kephra::Edit::Goto::_pos( $ep->GetSelectionEnd );
388 0           $return = _find_next();
389 0 0         if ( $return == -1 ) {
390 0 0         if ( get_range() eq 'document' ) {
    0          
391 0 0         $return = _find_first() if $attr->{auto_wrap};
392             } elsif ( get_range() eq 'open_docs' ) {
393 0           $Kephra::temp{dialog}{control} = 1;
394 0           my $begin_doc = Kephra::Document::Data::current_nr();
395 0           while ( $return == -1 ) {
396 0           Kephra::Edit::_restore_positions();
397 0 0 0       last if Kephra::Document::Data::current_nr()
398             == Kephra::Document::Data::last_nr()
399             and not $attr->{auto_wrap};
400 0           Kephra::Document::Change::tab_right();
401 0           Kephra::Edit::_save_positions();
402 0           $return = _find_first();
403 0 0         last if Kephra::Document::Data::current_nr() == $begin_doc;
404             }
405 0           $Kephra::temp{dialog}{control} = 0;
406             }
407             }
408 0 0         refresh_find_history($return) if $_ == 1;
409 0 0         if ( $return == -1 ) { Kephra::Edit::_restore_positions(); last; }
  0            
  0            
410 0           else { _caret_2_sel_end(); Kephra::Edit::_center_caret(); }
  0            
411             }
412             }
413             }
414            
415             sub find_first {
416 0     0 0   my $menu_call = shift;
417 0           my $ep = Kephra::App::EditPanel::_ref();
418 0           my $attr = _attributes();
419 0           my ( $sel_begin, $sel_end ) = $ep->GetSelection;
420 0           my $pos = $ep->GetCurrentPos;
421 0           my $len = _exist_find_item();
422 0           my $return;
423 0 0         if ( _exist_find_item() ) {
424 0           Kephra::Edit::_save_positions();
425 0 0 0       if ($menu_call
      0        
426             and $sel_begin != $sel_end
427             and $sel_end - $sel_begin > $len ) {
428 0           set_range('selection')
429             }
430 0 0         if ( get_range() eq 'selection' ) {
431 0           Kephra::Edit::Goto::_pos($sel_begin);
432 0           $return = _find_next();
433 0 0 0       if ($return > -1 and $ep->GetCurrentPos + $len <= $sel_end) {
434 0           Kephra::Edit::_center_caret();
435             } else {
436 0           Kephra::Edit::_restore_positions();
437 0           $return = -1;
438             }
439             } else {
440 0           $return = _find_first();
441 0 0 0       if ( get_range() eq 'open_docs'
      0        
442             and ($sel_begin == $ep->GetSelectionStart or $return == -1 ) ){
443 0           $Kephra::temp{dialog}{control} = 1;
444 0           $return = -1;
445 0           my $begin_doc = Kephra::Document::Data::current_nr();
446 0           while ( $return == -1 ) {
447 0           Kephra::Edit::_restore_positions();
448 0 0 0       last if Kephra::Document::Data::current_nr() == 0
449             and not $attr->{auto_wrap};
450 0           Kephra::Document::Change::tab_left();
451 0           Kephra::Edit::_save_positions();
452 0           $return = _find_first();
453 0 0         last if ( Kephra::Document::Data::current_nr() == $begin_doc );
454             }
455 0           $Kephra::temp{dialog}{control} = 0;
456             }
457 0 0         if ( $return > -1 ) {
458 0           _caret_2_sel_end();
459 0           Kephra::Edit::_center_caret();
460             } else {
461 0           Kephra::Edit::_restore_positions();
462             }
463             }
464 0           refresh_find_history($return);
465             }
466 0           $return;
467             }
468            
469             sub find_last {
470 0     0 0   my $menu_call = shift;
471 0           my $ep = Kephra::App::EditPanel::_ref();
472 0           my $attr = _attributes();
473 0           my ( $sel_begin, $sel_end ) = $ep->GetSelection;
474 0           my $pos = $ep->GetCurrentPos;
475 0           my $len = _exist_find_item();
476 0           my $return;
477 0 0         if (_exist_find_item()) {
478 0           Kephra::Edit::_save_positions();
479 0 0 0       if ($menu_call
      0        
480             and $sel_begin != $sel_end
481             and $sel_end - $sel_begin > $len) {
482 0           set_range('selection');
483             }
484 0 0         if ( get_range() eq 'selection' ) {
485 0           Kephra::Edit::Goto::_pos($sel_end);
486 0           $return = _find_prev();
487 0 0 0       if ($return > -1 and $ep->GetCurrentPos >= $sel_begin) {
488 0           Kephra::Edit::_center_caret();
489             } else {
490 0           Kephra::Edit::_restore_positions();
491 0           $return = -1;
492             }
493             } else {
494 0           $return = _find_last();
495 0 0 0       if (get_range() eq 'open_docs'
      0        
496             and ($sel_begin == $ep->GetSelectionStart or $return == -1) ){
497 0           $Kephra::temp{dialog}{control} = 1;
498 0           $return = -1;
499 0           my $begin_doc = Kephra::Document::Data::current_nr();
500 0           while ( $return == -1 ) {
501 0           Kephra::Edit::_restore_positions();
502 0 0 0       last if Kephra::Document::Data::current_nr()
503             == Kephra::Document::Data::last_nr()
504             and not $attr->{auto_wrap};
505 0           Kephra::Document::Change::tab_right();
506 0           Kephra::Edit::_save_positions();
507 0           $return = _find_last();
508 0 0         last if ( Kephra::Document::Data::current_nr() == $begin_doc );
509             }
510 0           $Kephra::temp{dialog}{control} = 0;
511             }
512 0 0         if ( $return > -1 ) {
513 0           _caret_2_sel_end();
514 0           Kephra::Edit::_center_caret();
515             } else {
516 0           Kephra::Edit::_restore_positions();
517             }
518             }
519 0           refresh_find_history($return);
520             }
521 0           $return;
522             }
523            
524             # replace
525             sub replace_back {
526 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
527 0 0         if ( $ep->GetSelectionStart != $ep->GetSelectionEnd ) {
528 0           replace_selection();
529 0           refresh_replace_history();
530 0           find_prev();
531             }
532             }
533            
534             sub replace_fore {
535 0     0 0   my $ep = Kephra::App::EditPanel::_ref();
536 0 0         if ( $ep->GetSelectionStart != $ep->GetSelectionEnd ) {
537 0           replace_selection();
538 0           refresh_replace_history();
539 0           find_next();
540             }
541             }
542            
543             sub replace_all {
544 0     0 0   my $menu_call = shift;
545 0           my $ep = Kephra::App::EditPanel::_ref();
546 0           my ($sel_begin, $sel_end ) = $ep->GetSelection;
547 0           my $line = $ep->GetCurrentLine;
548 0           my $len = _exist_find_item();
549 0           my $replace_string = get_replace_item();
550             #if ($len) { # forbid replace with nothing
551 0 0 0       if ( $menu_call
      0        
552             and $sel_begin != $sel_end
553             and $sel_end - $sel_begin > $len ) {
554 0           _attributes()->{in} = 'selection';
555             }
556 0 0         if ( get_range() eq 'selection' ) {
    0          
    0          
557 0           $ep->BeginUndoAction;
558 0           Kephra::Edit::Goto::_pos($sel_begin);
559 0           while ( _find_next() > -1 ) {
560 0 0         last if ( $ep->GetCurrentPos + $len >= $sel_end );
561 0           $ep->ReplaceSelection($replace_string);
562             }
563 0           $ep->EndUndoAction;
564             } elsif ( get_range() eq 'document' ) {
565 0           $ep->BeginUndoAction;
566 0           Kephra::Edit::Goto::_pos(0);
567 0           while ( _find_next() > -1 ) {
568 0           $ep->ReplaceSelection($replace_string);
569             }
570 0           $ep->EndUndoAction;
571             } elsif ( get_range() eq 'open_docs' ) {
572 0           my $begin_doc = Kephra::Document::Data::current_nr();
573 0           do {
574             {
575 0           Kephra::Edit::_save_positions();
  0            
576 0           $ep->BeginUndoAction;
577 0           Kephra::Edit::Goto::_pos(0);
578 0           while ( _find_next() > -1 ) {
579 0           $ep->ReplaceSelection($replace_string);
580             }
581 0           $ep->EndUndoAction;
582 0           Kephra::Edit::_restore_positions();
583             }
584             } until ( Kephra::Document::Change::tab_right() == $begin_doc );
585             }
586 0           $ep->GotoLine($line);
587 0           refresh_replace_history;
588 0           Kephra::Edit::_keep_focus();
589             #} # end of don't replace nothing
590             }
591            
592             sub replace_confirm {
593 0 0   0 0   if (_exist_find_item()) {
594 0           my $ep = Kephra::App::EditPanel::_ref();
595 0           my $attr = _attributes();
596 0           my $line = $ep->LineFromPosition( $ep->GetCurrentPos );
597 0           my $len = _exist_find_item();
598 0           my $sel_begin = $ep->GetSelectionStart;
599 0           my $sel_end = $ep->GetSelectionEnd;
600 0           my $answer = &Wx::wxYES;
601 0           my $menu_call = shift;
602            
603 0 0 0       set_range('selection')
      0        
604             if $menu_call
605             and $sel_begin != $sel_end
606             and $sel_end - $sel_begin > $len;
607            
608 0 0         if (get_range() eq 'selection') {
    0          
    0          
609 0           sniff_selection( $ep, $sel_begin, $sel_end, $len, $line );
610             } elsif (get_range() eq 'document') {
611 0           sniff_selection( $ep, 0, $ep->GetTextLength, $len, $line );
612             } elsif (get_range() eq 'open_docs') {
613 0           my $begin_doc = Kephra::Document::Data::current_nr();
614 0           do {
615             {
616 0 0         next if $answer == &Wx::wxCANCEL;
  0            
617 0           Kephra::Edit::_save_positions();
618 0           $answer = sniff_selection
619             ( $ep, 0, $ep->GetTextLength, $len, $line );
620 0           Kephra::Edit::_restore_positions();
621             }
622             } until ( Kephra::Document::Change::tab_right() == $begin_doc );
623             }
624             }
625            
626             sub sniff_selection {
627 0     0 0   my ( $ep, $sel_begin, $sel_end, $len, $line ) = @_;
628 0           my $l10n = Kephra::Config::Localisation::strings()->{dialog}{search}{confirm};
629 0           my $answer;
630 0           Kephra::Edit::Goto::_pos($sel_begin);
631 0           $ep->BeginUndoAction();
632 0           while ( _find_next() > -1 ) {
633 0 0         last if $ep->GetCurrentPos + $len >= $sel_end;
634 0           Kephra::Edit::_center_caret();
635 0           $answer = Kephra::Dialog::get_confirm_3
636             ($l10n->{text}, $l10n->{title}, 100, 100);
637 0 0         last if $answer == &Wx::wxCANCEL;
638 0 0         if ($answer == &Wx::wxYES) {replace_selection()}
  0            
  0            
639             else {$ep->SetCurrentPos( $ep->GetCurrentPos + 1 )}
640             }
641 0           $ep->EndUndoAction;
642 0           Kephra::Edit::Goto::_pos( $ep->PositionFromLine($line) );
643 0           Kephra::Edit::_center_caret();
644 0           $answer;
645             }
646 0           refresh_replace_history();
647 0           Kephra::Edit::_keep_focus();
648             }
649            
650             1;
651            
652             =head1 NAME
653            
654             Kephra::Edit::Search - find and replace functions
655            
656             =head1 DESCRIPTION
657            
658             =cut