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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include <com/sun/star/util/SearchFlags.hpp>
#include <com/sun/star/util/SearchResult.hpp>
#include <comphelper/lok.hxx>
#include <o3tl/safeint.hxx>
#include <rtl/ustrbuf.hxx>
#include <svx/svdview.hxx>
#include <svl/srchitem.hxx>
#include <sfx2/sfxsids.hrc>
#include <editeng/outliner.hxx>
#include <osl/diagnose.h>
#include <wrtsh.hxx>
#include <txatritr.hxx>
#include <fldbas.hxx>
#include <fmtfld.hxx>
#include <txtfld.hxx>
#include <txtfrm.hxx>
#include <rootfrm.hxx>
#include <swcrsr.hxx>
#include <redline.hxx>
#include <doc.hxx>
#include <IDocumentUndoRedo.hxx>
#include <IDocumentState.hxx>
#include <IDocumentDrawModelAccess.hxx>
#include <IDocumentRedlineAccess.hxx>
#include <dcontact.hxx>
#include <pamtyp.hxx>
#include <ndtxt.hxx>
#include <swundo.hxx>
#include <UndoInsert.hxx>
#include <breakit.hxx>
#include <docsh.hxx>
#include <PostItMgr.hxx>
#include <view.hxx>
using namespace ::com::sun::star;
using namespace util;
namespace {
/// because the Find may be called on the View or the Model, we need an index
/// afflicted by multiple personality disorder
struct AmbiguousIndex
{
private:
sal_Int32 m_value;
#ifndef NDEBUG
enum class tags : char { Any, Frame, Model };
tags m_tag;
#endif
public:
AmbiguousIndex() : m_value(-1)
#ifndef NDEBUG
, m_tag(tags::Any)
#endif
{}
explicit AmbiguousIndex(sal_Int32 const value
#ifndef NDEBUG
, tags const tag
#endif
) : m_value(value)
#ifndef NDEBUG
, m_tag(tag)
#endif
{}
sal_Int32 & GetAnyIndex() { return m_value; } ///< for arithmetic
sal_Int32 const& GetAnyIndex() const { return m_value; } ///< for arithmetic
TextFrameIndex GetFrameIndex() const
{
assert(m_tag != tags::Model);
return TextFrameIndex(m_value);
}
sal_Int32 GetModelIndex() const
{
assert(m_tag != tags::Frame);
return m_value;
}
void SetFrameIndex(TextFrameIndex const value)
{
#ifndef NDEBUG
m_tag = tags::Frame;
#endif
m_value = sal_Int32(value);
}
void SetModelIndex(sal_Int32 const value)
{
#ifndef NDEBUG
m_tag = tags::Model;
#endif
m_value = value;
}
bool operator ==(AmbiguousIndex const& rOther) const
{
assert(m_tag == tags::Any || rOther.m_tag == tags::Any || m_tag == rOther.m_tag);
return m_value == rOther.m_value;
}
bool operator <=(AmbiguousIndex const& rOther) const
{
assert(m_tag == tags::Any || rOther.m_tag == tags::Any || m_tag == rOther.m_tag);
return m_value <= rOther.m_value;
}
bool operator < (AmbiguousIndex const& rOther) const
{
assert(m_tag == tags::Any || rOther.m_tag == tags::Any || m_tag == rOther.m_tag);
return m_value < rOther.m_value;
}
AmbiguousIndex operator - (AmbiguousIndex const& rOther) const
{
assert(m_tag == tags::Any || rOther.m_tag == tags::Any || m_tag == rOther.m_tag);
return AmbiguousIndex(m_value - rOther.m_value
#ifndef NDEBUG
, std::max(m_tag, rOther.m_tag)
#endif
);
}
};
class MaybeMergedIter
{
std::optional<sw::MergedAttrIter> m_oMergedIter;
SwTextNode const*const m_pNode;
size_t m_HintIndex;
public:
MaybeMergedIter(SwTextFrame const*const pFrame, SwTextNode const*const pNode)
: m_pNode(pNode)
, m_HintIndex(0)
{
if (pFrame)
{
m_oMergedIter.emplace(*pFrame);
}
}
SwTextAttr const* NextAttr(SwTextNode const*& rpNode)
{
if (m_oMergedIter)
{
return m_oMergedIter->NextAttr(&rpNode);
}
if (SwpHints const*const pHints = m_pNode->GetpSwpHints())
{
if (m_HintIndex < pHints->Count())
{
rpNode = m_pNode;
return pHints->Get(m_HintIndex++);
}
}
return nullptr;
}
};
}
static OUString
lcl_CleanStr(const SwTextNode& rNd,
SwTextFrame const*const pFrame,
SwRootFrame const*const pLayout,
AmbiguousIndex const nStart, AmbiguousIndex & rEnd,
std::vector<AmbiguousIndex> &rArr,
bool const bRemoveSoftHyphen, bool const bRemoveCommentAnchors)
{
OUStringBuffer buf(pLayout ? pFrame->GetText() : rNd.GetText());
rArr.clear();
MaybeMergedIter iter(pLayout ? pFrame : nullptr, pLayout ? nullptr : &rNd);
AmbiguousIndex nSoftHyphen = nStart;
AmbiguousIndex nHintStart;
bool bNewHint = true;
bool bNewSoftHyphen = true;
const AmbiguousIndex nEnd = rEnd;
std::vector<AmbiguousIndex> aReplaced;
SwTextNode const* pNextHintNode(nullptr);
SwTextAttr const* pNextHint(iter.NextAttr(pNextHintNode));
do
{
if ( bNewHint )
{
if (pLayout)
{
nHintStart.SetFrameIndex(pNextHint
? pFrame->MapModelToView(pNextHintNode, pNextHint->GetStart())
: TextFrameIndex(-1));
}
else
{
nHintStart.SetModelIndex(pNextHint ? pNextHint->GetStart() : -1);
}
}
if ( bNewSoftHyphen )
{
if (pLayout)
{
nSoftHyphen.SetFrameIndex(TextFrameIndex(bRemoveSoftHyphen
? pFrame->GetText().indexOf(CHAR_SOFTHYPHEN, nSoftHyphen.GetAnyIndex())
: -1));
}
else
{
nSoftHyphen.SetModelIndex(bRemoveSoftHyphen
? rNd.GetText().indexOf(CHAR_SOFTHYPHEN, nSoftHyphen.GetAnyIndex())
: -1);
}
}
bNewHint = false;
bNewSoftHyphen = false;
AmbiguousIndex nStt;
// Check if next stop is a hint.
if (0 <= nHintStart.GetAnyIndex()
&& (-1 == nSoftHyphen.GetAnyIndex() || nHintStart < nSoftHyphen)
&& nHintStart < nEnd )
{
nStt = nHintStart;
bNewHint = true;
}
// Check if next stop is a soft hyphen.
else if ( -1 != nSoftHyphen.GetAnyIndex()
&& (-1 == nHintStart.GetAnyIndex() || nSoftHyphen < nHintStart)
&& nSoftHyphen < nEnd)
{
nStt = nSoftHyphen;
bNewSoftHyphen = true;
}
// If nSoftHyphen == nHintStart, the current hint *must* be a hint with an end.
else if (-1 != nSoftHyphen.GetAnyIndex() && nSoftHyphen == nHintStart)
{
nStt = nSoftHyphen;
bNewHint = true;
bNewSoftHyphen = true;
}
else
break;
AmbiguousIndex nCurrent(nStt);
nCurrent.GetAnyIndex() -= rArr.size();
if ( bNewHint )
{
if (pNextHint && pNextHint->HasDummyChar() && (nStart <= nStt))
{
switch (pNextHint->Which())
{
case RES_TXTATR_FLYCNT:
case RES_TXTATR_FIELD:
case RES_TXTATR_REFMARK:
case RES_TXTATR_TOXMARK:
case RES_TXTATR_META:
case RES_TXTATR_METAFIELD:
{
// (1998) they are desired as separators and
// belong not any longer to a word.
// they should also be ignored at a
// beginning/end of a sentence if blank. Those are
// simply removed if first. If at the end, we keep the
// replacement and remove afterwards all at a string's
// end (might be normal 0x7f).
const bool bEmpty = pNextHint->Which() != RES_TXTATR_FIELD
|| (static_txtattr_cast<SwTextField const*>(pNextHint)->GetFormatField().GetField()->ExpandField(true, pLayout).isEmpty());
if ( bEmpty && nStart == nCurrent )
{
rArr.push_back( nCurrent );
if (rEnd.GetAnyIndex() > nCurrent.GetAnyIndex())
{
--rEnd.GetAnyIndex();
}
buf.remove(nCurrent.GetAnyIndex(), 1);
}
else
{
if ( bEmpty )
aReplaced.push_back( nCurrent );
buf[nCurrent.GetAnyIndex()] = '\x7f';
}
}
break;
case RES_TXTATR_ANNOTATION:
{
if( bRemoveCommentAnchors )
{
rArr.push_back( nCurrent );
if (rEnd.GetAnyIndex() > nCurrent.GetAnyIndex())
{
--rEnd.GetAnyIndex();
}
buf.remove( nCurrent.GetAnyIndex(), 1 );
}
}
break;
default:
OSL_FAIL( "unknown case in lcl_CleanStr" );
break;
}
}
pNextHint = iter.NextAttr(pNextHintNode);
}
if ( bNewSoftHyphen )
{
rArr.push_back( nCurrent );
// If the soft hyphen to be removed is past the end of the range we're searching in,
// don't adjust the end.
if (rEnd.GetAnyIndex() > nCurrent.GetAnyIndex())
{
--rEnd.GetAnyIndex();
}
buf.remove(nCurrent.GetAnyIndex(), 1);
++nSoftHyphen.GetAnyIndex();
}
}
while ( true );
for (auto i = aReplaced.size(); i; )
{
const AmbiguousIndex nTmp = aReplaced[ --i ];
if (nTmp.GetAnyIndex() == buf.getLength() - 1)
{
buf.truncate(nTmp.GetAnyIndex());
rArr.push_back( nTmp );
--rEnd.GetAnyIndex();
}
}
return buf.makeStringAndClear();
}
static bool DoSearch(SwPaM & rSearchPam,
const i18nutil::SearchOptions2& rSearchOpt, utl::TextSearch& rSText,
SwMoveFnCollection const & fnMove,
bool bSrchForward, bool bRegSearch, bool bChkEmptyPara, bool bChkParaEnd,
AmbiguousIndex & nStart, AmbiguousIndex & nEnd, AmbiguousIndex nTextLen,
SwTextNode const* pNode, SwTextFrame const* pTextFrame,
SwRootFrame const* pLayout, SwPaM* pPam);
namespace sw {
// @param xSearchItem allocate in parent so we can do so outside the calling loop
bool FindTextImpl(SwPaM & rSearchPam,
const i18nutil::SearchOptions2& rSearchOpt, bool bSearchInNotes,
utl::TextSearch& rSText,
SwMoveFnCollection const & fnMove, const SwPaM & rRegion,
bool bInReadOnly, SwRootFrame const*const pLayout,
std::unique_ptr<SvxSearchItem>& xSearchItem)
{
if( rSearchOpt.searchString.isEmpty() )
return false;
std::unique_ptr<SwPaM> pPam = sw::MakeRegion(fnMove, rRegion);
const bool bSrchForward = &fnMove == &fnMoveForward;
SwNodeIndex& rNdIdx = pPam->GetPoint()->nNode;
SwIndex& rContentIdx = pPam->GetPoint()->nContent;
// If bFound is true then the string was found and is between nStart and nEnd
bool bFound = false;
// start position in text or initial position
bool bFirst = true;
SwContentNode * pNode;
const bool bRegSearch = SearchAlgorithms2::REGEXP == rSearchOpt.AlgorithmType2;
const bool bChkEmptyPara = bRegSearch && 2 == rSearchOpt.searchString.getLength() &&
( rSearchOpt.searchString == "^$" ||
rSearchOpt.searchString == "$^" );
const bool bChkParaEnd = bRegSearch && rSearchOpt.searchString == "$";
if (!xSearchItem)
{
xSearchItem.reset(new SvxSearchItem(SID_SEARCH_ITEM)); // this is a very expensive operation (calling configmgr etc.)
xSearchItem->SetSearchOptions(rSearchOpt);
xSearchItem->SetBackward(!bSrchForward);
}
// LanguageType eLastLang = 0;
while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
{
if( pNode->IsTextNode() )
{
SwTextNode& rTextNode = *pNode->GetTextNode();
SwTextFrame const*const pFrame(pLayout
? static_cast<SwTextFrame const*>(rTextNode.getLayoutFrame(pLayout))
: nullptr);
assert(!pLayout || pFrame);
AmbiguousIndex nTextLen;
if (pLayout)
{
nTextLen.SetFrameIndex(TextFrameIndex(pFrame->GetText().getLength()));
}
else
{
nTextLen.SetModelIndex(rTextNode.GetText().getLength());
}
AmbiguousIndex nEnd;
if (pLayout
? FrameContainsNode(*pFrame, pPam->GetMark()->nNode.GetIndex())
: rNdIdx == pPam->GetMark()->nNode)
{
if (pLayout)
{
nEnd.SetFrameIndex(pFrame->MapModelToViewPos(*pPam->GetMark()));
}
else
{
nEnd.SetModelIndex(pPam->GetMark()->nContent.GetIndex());
}
}
else
{
if (bSrchForward)
{
nEnd = nTextLen;
}
else
{
if (pLayout)
{
nEnd.SetFrameIndex(TextFrameIndex(0));
}
else
{
nEnd.SetModelIndex(0);
}
}
}
AmbiguousIndex nStart;
if (pLayout)
{
nStart.SetFrameIndex(pFrame->MapModelToViewPos(*pPam->GetPoint()));
}
else
{
nStart.SetModelIndex(rContentIdx.GetIndex());
}
/* #i80135# */
// if there are SwPostItFields inside our current node text, we
// split the text into separate pieces and search for text inside
// the pieces as well as inside the fields
MaybeMergedIter iter(pLayout ? pFrame : nullptr, pLayout ? nullptr : &rTextNode);
// count PostItFields by looping over all fields
std::vector<std::pair<SwTextAttr const*, AmbiguousIndex>> postits;
if (bSearchInNotes)
{
if (!bSrchForward)
{
std::swap(nStart, nEnd);
}
SwTextNode const* pTemp(nullptr);
while (SwTextAttr const*const pTextAttr = iter.NextAttr(pTemp))
{
if ( pTextAttr->Which()==RES_TXTATR_ANNOTATION )
{
AmbiguousIndex aPos;
aPos.SetModelIndex(pTextAttr->GetStart());
if (pLayout)
{
aPos.SetFrameIndex(pFrame->MapModelToView(pTemp, aPos.GetModelIndex()));
}
if ((nStart <= aPos) && (aPos <= nEnd))
{
postits.emplace_back(pTextAttr, aPos);
}
}
}
if (!bSrchForward)
{
std::swap(nStart, nEnd);
}
}
SwDocShell *const pDocShell = pNode->GetDoc().GetDocShell();
SwWrtShell *const pWrtShell = pDocShell ? pDocShell->GetWrtShell() : nullptr;
SwPostItMgr *const pPostItMgr = pWrtShell ? pWrtShell->GetPostItMgr() : nullptr;
// If there is an active text edit, then search there.
bool bEndedTextEdit = false;
SdrView* pSdrView = pWrtShell ? pWrtShell->GetDrawView() : nullptr;
if (pSdrView)
{
// If the edited object is not anchored to this node, then ignore it.
SdrObject* pObject = pSdrView->GetTextEditObject();
if (pObject)
{
if (SwFrameFormat* pFrameFormat = FindFrameFormat(pObject))
{
const SwPosition* pPosition = pFrameFormat->GetAnchor().GetContentAnchor();
if (!pPosition || (pLayout
? !FrameContainsNode(*pFrame, pPosition->nNode.GetIndex())
: pPosition->nNode.GetIndex() != pNode->GetIndex()))
pObject = nullptr;
}
}
if (pObject)
{
sal_uInt16 nResult = pSdrView->GetTextEditOutlinerView()->StartSearchAndReplace(*xSearchItem);
if (!nResult)
{
// If not found, end the text edit.
pSdrView->SdrEndTextEdit();
const Point aPoint(pSdrView->GetAllMarkedRect().TopLeft());
pSdrView->UnmarkAll();
pWrtShell->CallSetCursor(&aPoint, true);
pWrtShell->Edit();
bEndedTextEdit = true;
}
else
{
bFound = true;
break;
}
}
}
if (comphelper::LibreOfficeKit::isActive())
{
// Writer and editeng selections are not supported in parallel.
SvxSearchItem* pSearchItem = SwView::GetSearchItem();
// If we just finished search in shape text, don't attempt to do that again.
if (!bEndedTextEdit && !(pSearchItem && pSearchItem->GetCommand() == SvxSearchCmd::FIND_ALL))
{
// If there are any shapes anchored to this node, search there.
SwPaM aPaM(pNode->GetDoc().GetNodes().GetEndOfContent());
if (pLayout)
{
*aPaM.GetPoint() = pFrame->MapViewToModelPos(nStart.GetFrameIndex());
}
else
{
aPaM.GetPoint()->nNode = rTextNode;
aPaM.GetPoint()->nContent.Assign(
aPaM.GetPoint()->nNode.GetNode().GetTextNode(),
nStart.GetModelIndex());
}
aPaM.SetMark();
if (pLayout)
{
aPaM.GetMark()->nNode = (pFrame->GetMergedPara()
? *pFrame->GetMergedPara()->pLastNode
: rTextNode)
.GetIndex() + 1;
}
else
{
aPaM.GetMark()->nNode = rTextNode.GetIndex() + 1;
}
aPaM.GetMark()->nContent.Assign(aPaM.GetMark()->nNode.GetNode().GetTextNode(), 0);
if (pNode->GetDoc().getIDocumentDrawModelAccess().Search(aPaM, *xSearchItem) && pSdrView)
{
if (SdrObject* pObject = pSdrView->GetTextEditObject())
{
if (SwFrameFormat* pFrameFormat = FindFrameFormat(pObject))
{
const SwPosition* pPosition = pFrameFormat->GetAnchor().GetContentAnchor();
if (pPosition)
{
// Set search position to the shape's anchor point.
*rSearchPam.GetPoint() = *pPosition;
rSearchPam.GetPoint()->nContent.Assign(pPosition->nNode.GetNode().GetContentNode(), 0);
rSearchPam.SetMark();
bFound = true;
break;
}
}
}
}
}
}
// do we need to finish a note?
if (pPostItMgr && pPostItMgr->HasActiveSidebarWin())
{
if (bSearchInNotes)
{
if (!postits.empty())
{
if (bSrchForward)
{
postits.erase(postits.begin());
}
else
{
postits.pop_back(); // hope that's the right one?
}
}
//search inside, finish and put focus back into the doc
if (pPostItMgr->FinishSearchReplace(rSearchOpt,bSrchForward))
{
bFound = true ;
break;
}
}
else
{
pPostItMgr->SetActiveSidebarWin(nullptr);
}
}
if (!postits.empty())
{
// now we have to split
AmbiguousIndex nStartInside;
AmbiguousIndex nEndInside;
sal_Int32 aLoop = bSrchForward ? 0 : postits.size();
while ((0 <= aLoop) && (o3tl::make_unsigned(aLoop) <= postits.size()))
{
if (bSrchForward)
{
if (aLoop == 0)
{
nStartInside = nStart;
}
else if (pLayout)
{
nStartInside.SetFrameIndex(postits[aLoop - 1].second.GetFrameIndex() + TextFrameIndex(1));
}
else
{
nStartInside.SetModelIndex(postits[aLoop - 1].second.GetModelIndex() + 1);
}
nEndInside = static_cast<size_t>(aLoop) == postits.size()
? nEnd
: postits[aLoop].second;
nTextLen = nEndInside - nStartInside;
}
else
{
nStartInside = static_cast<size_t>(aLoop) == postits.size()
? nStart
: postits[aLoop].second;
if (aLoop == 0)
{
nEndInside = nEnd;
}
else if (pLayout)
{
nEndInside.SetFrameIndex(postits[aLoop - 1].second.GetFrameIndex() + TextFrameIndex(1));
}
else
{
nEndInside.SetModelIndex(postits[aLoop - 1].second.GetModelIndex() + 1);
}
nTextLen = nStartInside - nEndInside;
}
// search inside the text between a note
bFound = DoSearch( rSearchPam,
rSearchOpt, rSText, fnMove, bSrchForward,
bRegSearch, bChkEmptyPara, bChkParaEnd,
nStartInside, nEndInside, nTextLen,
pNode->GetTextNode(), pFrame, pLayout,
pPam.get() );
if ( bFound )
break;
else
{
// we should now be right in front of a note, search inside
if (bSrchForward
? (static_cast<size_t>(aLoop) != postits.size())
: (aLoop != 0))
{
const SwTextAttr *const pTextAttr = bSrchForward
? postits[aLoop].first
: postits[aLoop - 1].first;
if (pPostItMgr && pPostItMgr->SearchReplace(
static_txtattr_cast<SwTextField const*>(pTextAttr)->GetFormatField(),rSearchOpt,bSrchForward))
{
bFound = true ;
break;
}
}
}
aLoop = bSrchForward ? aLoop+1 : aLoop-1;
}
}
else
{
// if there is no SwPostItField inside or searching inside notes
// is disabled, we search the whole length just like before
bFound = DoSearch( rSearchPam,
rSearchOpt, rSText, fnMove, bSrchForward,
bRegSearch, bChkEmptyPara, bChkParaEnd,
nStart, nEnd, nTextLen,
pNode->GetTextNode(), pFrame, pLayout,
pPam.get() );
}
if (bFound)
break;
}
}
return bFound;
}
} // namespace sw
bool DoSearch(SwPaM & rSearchPam,
const i18nutil::SearchOptions2& rSearchOpt, utl::TextSearch& rSText,
SwMoveFnCollection const & fnMove, bool bSrchForward, bool bRegSearch,
bool bChkEmptyPara, bool bChkParaEnd,
AmbiguousIndex & nStart, AmbiguousIndex & nEnd, AmbiguousIndex const nTextLen,
SwTextNode const*const pNode, SwTextFrame const*const pFrame,
SwRootFrame const*const pLayout, SwPaM* pPam)
{
bool bFound = false;
SwNodeIndex& rNdIdx = pPam->GetPoint()->nNode;
OUString sCleanStr;
std::vector<AmbiguousIndex> aFltArr;
LanguageType eLastLang = LANGUAGE_SYSTEM;
// if the search string contains a soft hyphen,
// we don't strip them from the text:
bool bRemoveSoftHyphens = true;
// if the search string contains a comment, we don't strip them from the text
const bool bRemoveCommentAnchors = rSearchOpt.searchString.indexOf( CH_TXTATR_INWORD ) == -1;
if ( bRegSearch )
{
if ( -1 != rSearchOpt.searchString.indexOf("\\xAD")
|| -1 != rSearchOpt.searchString.indexOf("\\x{00AD}")
|| -1 != rSearchOpt.searchString.indexOf("\\u00AD")
|| -1 != rSearchOpt.searchString.indexOf("\\U000000AD")
|| -1 != rSearchOpt.searchString.indexOf("\\N{SOFT HYPHEN}"))
{
bRemoveSoftHyphens = false;
}
}
else
{
if ( 1 == rSearchOpt.searchString.getLength() &&
CHAR_SOFTHYPHEN == rSearchOpt.searchString.toChar() )
bRemoveSoftHyphens = false;
}
if( bSrchForward )
sCleanStr = lcl_CleanStr(*pNode, pFrame, pLayout, nStart, nEnd,
aFltArr, bRemoveSoftHyphens, bRemoveCommentAnchors);
else
sCleanStr = lcl_CleanStr(*pNode, pFrame, pLayout, nEnd, nStart,
aFltArr, bRemoveSoftHyphens, bRemoveCommentAnchors);
std::unique_ptr<SwScriptIterator> pScriptIter;
sal_uInt16 nSearchScript = 0;
sal_uInt16 nCurrScript = 0;
if (SearchAlgorithms2::APPROXIMATE == rSearchOpt.AlgorithmType2)
{
pScriptIter.reset(new SwScriptIterator(sCleanStr, nStart.GetAnyIndex(), bSrchForward));
nSearchScript = g_pBreakIt->GetRealScriptOfText( rSearchOpt.searchString, 0 );
}
const AmbiguousIndex nStringEnd = nEnd;
bool bZeroMatch = false; // zero-length match, i.e. only $ anchor as regex
while ( ((bSrchForward && nStart < nStringEnd) ||
(!bSrchForward && nStringEnd < nStart)) && !bZeroMatch )
{
// SearchAlgorithms_APPROXIMATE works on a per word base so we have to
// provide the text searcher with the correct locale, because it uses
// the break-iterator
if ( pScriptIter )
{
nEnd.GetAnyIndex() = pScriptIter->GetScriptChgPos();
nCurrScript = pScriptIter->GetCurrScript();
if ( nSearchScript == nCurrScript )
{
const LanguageType eCurrLang = pLayout
? pFrame->GetLangOfChar(bSrchForward
? nStart.GetFrameIndex()
: nEnd.GetFrameIndex(),
0, true)
: pNode->GetLang(bSrchForward
? nStart.GetModelIndex()
: nEnd.GetModelIndex());
if ( eCurrLang != eLastLang )
{
const lang::Locale aLocale(
g_pBreakIt->GetLocale( eCurrLang ) );
rSText.SetLocale( utl::TextSearch::UpgradeToSearchOptions2( rSearchOpt), aLocale );
eLastLang = eCurrLang;
}
}
pScriptIter->Next();
}
AmbiguousIndex nProxyStart = nStart;
AmbiguousIndex nProxyEnd = nEnd;
if( nSearchScript == nCurrScript &&
(rSText.*fnMove.fnSearch)( sCleanStr, &nProxyStart.GetAnyIndex(), &nProxyEnd.GetAnyIndex(), nullptr) &&
!(bZeroMatch = (nProxyStart == nProxyEnd)))
{
nStart = nProxyStart;
nEnd = nProxyEnd;
// set section correctly
*rSearchPam.GetPoint() = *pPam->GetPoint();
rSearchPam.SetMark();
// adjust start and end
if( !aFltArr.empty() )
{
// if backward search, switch positions temporarily
if (!bSrchForward) { std::swap(nStart, nEnd); }
AmbiguousIndex nNew = nStart;
for (size_t n = 0; n < aFltArr.size() && aFltArr[ n ] <= nStart; ++n )
{
++nNew.GetAnyIndex();
}
nStart = nNew;
nNew = nEnd;
for( size_t n = 0; n < aFltArr.size() && aFltArr[ n ] < nEnd; ++n )
{
++nNew.GetAnyIndex();
}
nEnd = nNew;
// if backward search, switch positions temporarily
if( !bSrchForward ) { std::swap(nStart, nEnd); }
}
if (pLayout)
{
*rSearchPam.GetMark() = pFrame->MapViewToModelPos(nStart.GetFrameIndex());
*rSearchPam.GetPoint() = pFrame->MapViewToModelPos(nEnd.GetFrameIndex());
}
else
{
rSearchPam.GetMark()->nContent = nStart.GetModelIndex();
rSearchPam.GetPoint()->nContent = nEnd.GetModelIndex();
}
// if backward search, switch point and mark
if( !bSrchForward )
rSearchPam.Exchange();
bFound = true;
break;
}
else
{
nEnd = nProxyEnd;
}
nStart = nEnd;
}
pScriptIter.reset();
if ( bFound )
return true;
else if ((bChkEmptyPara && !nStart.GetAnyIndex() && !nTextLen.GetAnyIndex())
|| bChkParaEnd)
{
*rSearchPam.GetPoint() = *pPam->GetPoint();
if (pLayout)
{
*rSearchPam.GetPoint() = pFrame->MapViewToModelPos(
bChkParaEnd ? nTextLen.GetFrameIndex() : TextFrameIndex(0));
}
else
{
rSearchPam.GetPoint()->nContent = bChkParaEnd ? nTextLen.GetModelIndex() : 0;
}
rSearchPam.SetMark();
const SwNode *const pSttNd = bSrchForward
? &rSearchPam.GetPoint()->nNode.GetNode() // end of the frame
: &rNdIdx.GetNode(); // keep the bug as-is for now...
/* FIXME: this condition does not work for !bSrchForward backward
* search, it probably never did. (pSttNd != &rNdIdx.GetNode())
* is never true in this case. */
if( (bSrchForward || pSttNd != &rNdIdx.GetNode()) &&
rSearchPam.Move(fnMoveForward, GoInContent) &&
(!bSrchForward || pSttNd != &rSearchPam.GetPoint()->nNode.GetNode()) &&
SwNodeOffset(1) == abs(rSearchPam.GetPoint()->nNode.GetIndex() -
rSearchPam.GetMark()->nNode.GetIndex()))
{
// if backward search, switch point and mark
if( !bSrchForward )
rSearchPam.Exchange();
return true;
}
}
return bFound;
}
namespace {
/// parameters for search and replace in text
struct SwFindParaText : public SwFindParas
{
const i18nutil::SearchOptions2& m_rSearchOpt;
SwCursor& m_rCursor;
SwRootFrame const* m_pLayout;
utl::TextSearch m_aSText;
bool m_bReplace;
bool m_bSearchInNotes;
SwFindParaText(const i18nutil::SearchOptions2& rOpt, bool bSearchInNotes,
bool bRepl, SwCursor& rCursor, SwRootFrame const*const pLayout)
: m_rSearchOpt( rOpt )
, m_rCursor( rCursor )
, m_pLayout(pLayout)
, m_aSText( utl::TextSearch::UpgradeToSearchOptions2(rOpt) )
, m_bReplace( bRepl )
, m_bSearchInNotes( bSearchInNotes )
{}
virtual int DoFind(SwPaM &, SwMoveFnCollection const &, const SwPaM &, bool bInReadOnly, std::unique_ptr<SvxSearchItem>& xSearchItem) override;
virtual bool IsReplaceMode() const override;
virtual ~SwFindParaText();
};
}
SwFindParaText::~SwFindParaText()
{
}
int SwFindParaText::DoFind(SwPaM & rCursor, SwMoveFnCollection const & fnMove,
const SwPaM & rRegion, bool bInReadOnly,
std::unique_ptr<SvxSearchItem>& xSearchItem)
{
if( bInReadOnly && m_bReplace )
bInReadOnly = false;
const bool bFnd = sw::FindTextImpl(rCursor, m_rSearchOpt, m_bSearchInNotes,
m_aSText, fnMove, rRegion, bInReadOnly, m_pLayout, xSearchItem);
if( bFnd && m_bReplace ) // replace string
{
// use replace method in SwDoc
const bool bRegExp(SearchAlgorithms2::REGEXP == m_rSearchOpt.AlgorithmType2);
SwIndex& rSttCntIdx = rCursor.Start()->nContent;
const sal_Int32 nSttCnt = rSttCntIdx.GetIndex();
// add to shell-cursor-ring so that the regions will be moved eventually
SwPaM* pPrev(nullptr);
if( bRegExp )
{
pPrev = const_cast<SwPaM&>(rRegion).GetPrev();
const_cast<SwPaM&>(rRegion).GetRingContainer().merge( m_rCursor.GetRingContainer() );
}
std::optional<OUString> xRepl;
if (bRegExp)
xRepl = sw::ReplaceBackReferences(m_rSearchOpt, &rCursor, m_pLayout);
bool const bReplaced = sw::ReplaceImpl(rCursor,
xRepl ? *xRepl : m_rSearchOpt.replaceString,
bRegExp, m_rCursor.GetDoc(), m_pLayout);
m_rCursor.SaveTableBoxContent( rCursor.GetPoint() );
if( bRegExp )
{
// and remove region again
SwPaM* p;
SwPaM* pNext(const_cast<SwPaM*>(&rRegion));
do {
p = pNext;
pNext = p->GetNext();
p->MoveTo(const_cast<SwPaM*>(&rRegion));
} while( p != pPrev );
}
if (bRegExp && !bReplaced)
{ // fdo#80715 avoid infinite loop if join failed
bool bRet = ((&fnMoveForward == &fnMove) ? &GoNextPara : &GoPrevPara)
(rCursor, fnMove);
(void) bRet;
assert(bRet); // if join failed, next node must be SwTextNode
}
else
rCursor.Start()->nContent = nSttCnt;
return FIND_NO_RING;
}
return bFnd ? FIND_FOUND : FIND_NOT_FOUND;
}
bool SwFindParaText::IsReplaceMode() const
{
return m_bReplace;
}
sal_uLong SwCursor::Find_Text( const i18nutil::SearchOptions2& rSearchOpt, bool bSearchInNotes,
SwDocPositions nStart, SwDocPositions nEnd,
bool& bCancel, FindRanges eFndRngs, bool bReplace,
SwRootFrame const*const pLayout)
{
// switch off OLE-notifications
SwDoc& rDoc = GetDoc();
Link<bool,void> aLnk( rDoc.GetOle2Link() );
rDoc.SetOle2Link( Link<bool,void>() );
bool const bStartUndo = rDoc.GetIDocumentUndoRedo().DoesUndo() && bReplace;
if (bStartUndo)
{
rDoc.GetIDocumentUndoRedo().StartUndo( SwUndoId::REPLACE, nullptr );
}
bool bSearchSel = 0 != (rSearchOpt.searchFlag & SearchFlags::REG_NOT_BEGINOFLINE);
if( bSearchSel )
eFndRngs = static_cast<FindRanges>(eFndRngs | FindRanges::InSel);
SwFindParaText aSwFindParaText(rSearchOpt, bSearchInNotes, bReplace, *this, pLayout);
sal_uLong nRet = FindAll( aSwFindParaText, nStart, nEnd, eFndRngs, bCancel );
rDoc.SetOle2Link( aLnk );
if( nRet && bReplace )
rDoc.getIDocumentState().SetModified();
if (bStartUndo)
{
SwRewriter rewriter(MakeUndoReplaceRewriter(
nRet, rSearchOpt.searchString, rSearchOpt.replaceString));
rDoc.GetIDocumentUndoRedo().EndUndo( SwUndoId::REPLACE, & rewriter );
}
return nRet;
}
namespace sw {
bool ReplaceImpl(
SwPaM & rCursor,
OUString const& rReplacement,
bool const bRegExp,
SwDoc & rDoc,
SwRootFrame const*const pLayout)
{
bool bReplaced(true);
IDocumentContentOperations & rIDCO(rDoc.getIDocumentContentOperations());
#if 0
// FIXME there's some problem with multiple redlines here on Undo
std::vector<std::shared_ptr<SwUnoCursor>> ranges;
if (rDoc.getIDocumentRedlineAccess().IsRedlineOn()
|| !pLayout
|| !pLayout->IsHideRedlines()
|| sw::GetRanges(ranges, rDoc, rCursor))
{
bReplaced = rIDCO.ReplaceRange(rCursor, rReplacement, bRegExp);
}
else
{
assert(!ranges.empty());
assert(ranges.front()->GetPoint()->nNode == ranges.front()->GetMark()->nNode);
bReplaced = rIDCO.ReplaceRange(*ranges.front(), rReplacement, bRegExp);
for (auto it = ranges.begin() + 1; it != ranges.end(); ++it)
{
bReplaced &= rIDCO.DeleteAndJoin(**it);
}
}
#else
IDocumentRedlineAccess const& rIDRA(rDoc.getIDocumentRedlineAccess());
if (pLayout && pLayout->IsHideRedlines()
&& !rIDRA.IsRedlineOn() // otherwise: ReplaceRange will handle it
&& (rIDRA.GetRedlineFlags() & RedlineFlags::ShowDelete)) // otherwise: ReplaceRange will DeleteRedline()
{
SwRedlineTable::size_type tmp;
rIDRA.GetRedline(*rCursor.Start(), &tmp);
while (tmp < rIDRA.GetRedlineTable().size())
{
SwRangeRedline const*const pRedline(rIDRA.GetRedlineTable()[tmp]);
if (*rCursor.End() <= *pRedline->Start())
{
break;
}
if (*pRedline->End() <= *rCursor.Start())
{
++tmp;
continue;
}
if (pRedline->GetType() == RedlineType::Delete)
{
assert(*pRedline->Start() != *pRedline->End());
// search in hidden layout can't overlap redlines
assert(*rCursor.Start() <= *pRedline->Start() && *pRedline->End() <= *rCursor.End());
SwPaM pam(*pRedline, nullptr);
bReplaced &= rIDCO.DeleteAndJoin(pam);
}
else
{
++tmp;
}
}
}
bReplaced &= rIDCO.ReplaceRange(rCursor, rReplacement, bRegExp);
#endif
return bReplaced;
}
std::optional<OUString> ReplaceBackReferences(const i18nutil::SearchOptions2& rSearchOpt,
SwPaM *const pPam, SwRootFrame const*const pLayout)
{
std::optional<OUString> xRet;
if( pPam && pPam->HasMark() &&
SearchAlgorithms2::REGEXP == rSearchOpt.AlgorithmType2 )
{
SwContentNode const*const pTextNode = pPam->GetContentNode();
SwContentNode const*const pMarkTextNode = pPam->GetContentNode(false);
if (!pTextNode || !pTextNode->IsTextNode()
|| !pMarkTextNode || !pMarkTextNode->IsTextNode())
{
return xRet;
}
SwTextFrame const*const pFrame(pLayout
? static_cast<SwTextFrame const*>(pTextNode->getLayoutFrame(pLayout))
: nullptr);
const bool bParaEnd = rSearchOpt.searchString == "$" || rSearchOpt.searchString == "^$" || rSearchOpt.searchString == "$^";
if (bParaEnd || (pLayout
? sw::FrameContainsNode(*pFrame, pPam->GetMark()->nNode.GetIndex())
: pTextNode == pMarkTextNode))
{
utl::TextSearch aSText( utl::TextSearch::UpgradeToSearchOptions2( rSearchOpt) );
SearchResult aResult;
OUString aReplaceStr( rSearchOpt.replaceString );
if (bParaEnd)
{
OUString const aStr("\\n");
aResult.subRegExpressions = 1;
aResult.startOffset = { 0 };
aResult.endOffset = { aStr.getLength() };
aSText.ReplaceBackReferences( aReplaceStr, aStr, aResult );
xRet = aReplaceStr;
}
else
{
AmbiguousIndex nStart;
AmbiguousIndex nEnd;
if (pLayout)
{
nStart.SetFrameIndex(pFrame->MapModelToViewPos(*pPam->Start()));
nEnd.SetFrameIndex(pFrame->MapModelToViewPos(*pPam->End()));
}
else
{
nStart.SetModelIndex(pPam->Start()->nContent.GetIndex());
nEnd.SetModelIndex(pPam->End()->nContent.GetIndex());
}
std::vector<AmbiguousIndex> aFltArr;
OUString const aStr = lcl_CleanStr(*pTextNode->GetTextNode(), pFrame, pLayout,
nStart, nEnd, aFltArr, false, false);
if (aSText.SearchForward(aStr, &nStart.GetAnyIndex(), &nEnd.GetAnyIndex(), &aResult))
{
aSText.ReplaceBackReferences( aReplaceStr, aStr, aResult );
xRet = aReplaceStr;
}
}
}
}
return xRet;
}
} // namespace sw
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|