LibreOffice Module sw (master)  1
findattr.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <com/sun/star/lang/Locale.hpp>
21 #include <com/sun/star/util/SearchAlgorithms2.hpp>
22 #include <com/sun/star/util/SearchFlags.hpp>
24 #include <i18nutil/searchopt.hxx>
25 #include <unotools/syslocale.hxx>
26 #include <hintids.hxx>
27 #include <svl/itemiter.hxx>
28 #include <svl/whiter.hxx>
29 #include <editeng/colritem.hxx>
30 #include <editeng/fontitem.hxx>
31 #include <fmtpdsc.hxx>
32 #include <txatbase.hxx>
33 #include <charfmt.hxx>
34 #include <crsrsh.hxx>
35 #include <doc.hxx>
36 #include <IDocumentUndoRedo.hxx>
37 #include <IDocumentState.hxx>
38 #include <swcrsr.hxx>
39 #include <ndtxt.hxx>
40 #include <pamtyp.hxx>
41 #include <txtfrm.hxx>
42 #include <swundo.hxx>
43 #include <optional>
44 
45 #include <algorithm>
46 #include <memory>
47 
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::util;
51 
53 
54 // Special case for SvxFontItem: only compare the name
55 static bool CmpAttr( const SfxPoolItem& rItem1, const SfxPoolItem& rItem2 )
56 {
57  switch( rItem1.Which() )
58  {
59  case RES_CHRATR_FONT:
60  return static_cast<const SvxFontItem&>(rItem1).GetFamilyName() ==
61  static_cast<const SvxFontItem&>(rItem2).GetFamilyName();
62 
63  case RES_CHRATR_COLOR:
64  return static_cast<const SvxColorItem&>(rItem1).GetValue().IsRGBEqual(
65  static_cast<const SvxColorItem&>(rItem2).GetValue() );
66  case RES_PAGEDESC:
67  ::std::optional<sal_uInt16> const oNumOffset1 =
68  static_cast<const SwFormatPageDesc&>(rItem1).GetNumOffset();
69  ::std::optional<sal_uInt16> const oNumOffset2 =
70  static_cast<const SwFormatPageDesc&>(rItem2).GetNumOffset();
71 
72  if (oNumOffset1 != oNumOffset2)
73  return false;
74 
75  return static_cast<const SwFormatPageDesc&>(rItem1).GetPageDesc() == static_cast<const SwFormatPageDesc&>(rItem2).GetPageDesc();
76  }
77  return rItem1 == rItem2;
78 }
79 
80 const SwTextAttr* GetFrwrdTextHint( const SwpHints& rHtsArr, size_t& rPos,
81  sal_Int32 nContentPos )
82 {
83  while( rPos < rHtsArr.Count() )
84  {
85  const SwTextAttr *pTextHt = rHtsArr.Get( rPos++ );
86  // the start of an attribute has to be in the section
87  if( pTextHt->GetStart() >= nContentPos )
88  return pTextHt; // valid text attribute
89  }
90  return nullptr; // invalid text attribute
91 }
92 
93 const SwTextAttr* GetBkwrdTextHint( const SwpHints& rHtsArr, size_t& rPos,
94  sal_Int32 nContentPos )
95 {
96  while( rPos > 0 )
97  {
98  const SwTextAttr *pTextHt = rHtsArr.Get( --rPos );
99  // the start of an attribute has to be in the section
100  if( pTextHt->GetStart() < nContentPos )
101  return pTextHt; // valid text attribute
102  }
103  return nullptr; // invalid text attribute
104 }
105 
106 static void lcl_SetAttrPam( SwPaM& rPam, sal_Int32 nStart, const sal_Int32* pEnd,
107  const bool bSaveMark )
108 {
109  sal_Int32 nContentPos;
110  if( bSaveMark )
111  nContentPos = rPam.GetMark()->nContent.GetIndex();
112  else
113  nContentPos = rPam.GetPoint()->nContent.GetIndex();
114  bool bTstEnd = rPam.GetPoint()->nNode == rPam.GetMark()->nNode;
115 
116  SwContentNode* pCNd = rPam.GetContentNode();
117  rPam.GetPoint()->nContent.Assign( pCNd, nStart );
118  rPam.SetMark(); // Point == GetMark
119 
120  // Point points to end of search area or end of attribute
121  if( pEnd )
122  {
123  if( bTstEnd && *pEnd > nContentPos )
124  rPam.GetPoint()->nContent = nContentPos;
125  else
126  rPam.GetPoint()->nContent = *pEnd;
127  }
128 }
129 
130 // TODO: provide documentation
143 static bool lcl_SearchAttr( const SwTextNode& rTextNd, SwPaM& rPam,
144  const SfxPoolItem& rCmpItem,
145  SwMoveFnCollection const & fnMove)
146 {
147  if ( !rTextNd.HasHints() )
148  return false;
149 
150  const SwTextAttr *pTextHt = nullptr;
151  bool bForward = &fnMove == &fnMoveForward;
152  size_t nPos = bForward ? 0 : rTextNd.GetSwpHints().Count();
153  sal_Int32 nContentPos = rPam.GetPoint()->nContent.GetIndex();
154 
155  while( nullptr != ( pTextHt=(*fnMove.fnGetHint)(rTextNd.GetSwpHints(),nPos,nContentPos)))
156  if (pTextHt->Which() == rCmpItem.Which())
157  {
158  lcl_SetAttrPam( rPam, pTextHt->GetStart(), pTextHt->End(), bForward );
159  return true;
160  }
161  return false;
162 }
163 
164 namespace {
165 
167 struct SwSrchChrAttr
168 {
169  sal_uInt16 nWhich;
170  sal_Int32 nStt;
171  sal_Int32 nEnd;
172 
173  SwSrchChrAttr(): nWhich(0), nStt(0), nEnd(0) {}
174 
175  SwSrchChrAttr( const SfxPoolItem& rItem,
176  sal_Int32 nStart, sal_Int32 nAnyEnd )
177  : nWhich( rItem.Which() ), nStt( nStart ), nEnd( nAnyEnd )
178  {}
179 };
180 
181 class SwAttrCheckArr
182 {
183  SwSrchChrAttr *m_pFindArr, *m_pStackArr;
184  sal_Int32 m_nNodeStart;
185  sal_Int32 m_nNodeEnd;
186  sal_uInt16 m_nArrStart, m_nArrLen;
187  sal_uInt16 m_nFound, m_nStackCount;
188  SfxItemSet m_aComapeSet;
189  bool m_bNoColls;
190  bool m_bForward;
191 
192 public:
193  SwAttrCheckArr( const SfxItemSet& rSet, bool bForward, bool bNoCollections );
194  ~SwAttrCheckArr();
195 
196  void SetNewSet( const SwTextNode& rTextNd, const SwPaM& rPam );
197 
199  sal_uInt16 Count() const { return m_aComapeSet.Count(); }
200  bool Found() const { return m_nFound == m_aComapeSet.Count(); }
201  bool CheckStack();
202 
203  sal_Int32 Start() const;
204  sal_Int32 End() const;
205 
206  sal_Int32 GetNdStt() const { return m_nNodeStart; }
207  sal_Int32 GetNdEnd() const { return m_nNodeEnd; }
208 
209  bool SetAttrFwd( const SwTextAttr& rAttr );
210  bool SetAttrBwd( const SwTextAttr& rAttr );
211 };
212 
213 }
214 
215 SwAttrCheckArr::SwAttrCheckArr( const SfxItemSet& rSet, bool bFwd,
216  bool bNoCollections )
217  : m_nNodeStart(0)
218  , m_nNodeEnd(0)
219  , m_nFound(0)
220  , m_nStackCount(0)
221  , m_aComapeSet( *rSet.GetPool(), svl::Items<RES_CHRATR_BEGIN, RES_TXTATR_END-1>{} )
222  , m_bNoColls(bNoCollections)
223  , m_bForward(bFwd)
224 {
225  m_aComapeSet.Put( rSet, false );
226 
227  // determine area of Fnd/Stack array (Min/Max)
228  SfxItemIter aIter( m_aComapeSet );
229  m_nArrStart = m_aComapeSet.GetWhichByPos( aIter.GetFirstPos() );
230  m_nArrLen = m_aComapeSet.GetWhichByPos( aIter.GetLastPos() ) - m_nArrStart+1;
231 
232  char* pFndChar = new char[ m_nArrLen * sizeof(SwSrchChrAttr) ];
233  char* pStackChar = new char[ m_nArrLen * sizeof(SwSrchChrAttr) ];
234 
235  m_pFindArr = reinterpret_cast<SwSrchChrAttr*>(pFndChar);
236  m_pStackArr = reinterpret_cast<SwSrchChrAttr*>(pStackChar);
237 }
238 
239 SwAttrCheckArr::~SwAttrCheckArr()
240 {
241  delete[] reinterpret_cast<char*>(m_pFindArr);
242  delete[] reinterpret_cast<char*>(m_pStackArr);
243 }
244 
245 void SwAttrCheckArr::SetNewSet( const SwTextNode& rTextNd, const SwPaM& rPam )
246 {
247  std::fill(m_pFindArr, m_pFindArr + m_nArrLen, SwSrchChrAttr());
248  std::fill(m_pStackArr, m_pStackArr + m_nArrLen, SwSrchChrAttr());
249  m_nFound = 0;
250  m_nStackCount = 0;
251 
252  if( m_bForward )
253  {
254  m_nNodeStart = rPam.GetPoint()->nContent.GetIndex();
255  m_nNodeEnd = rPam.GetPoint()->nNode == rPam.GetMark()->nNode
256  ? rPam.GetMark()->nContent.GetIndex()
257  : rTextNd.GetText().getLength();
258  }
259  else
260  {
261  m_nNodeEnd = rPam.GetPoint()->nContent.GetIndex();
262  m_nNodeStart = rPam.GetPoint()->nNode == rPam.GetMark()->nNode
263  ? rPam.GetMark()->nContent.GetIndex()
264  : 0;
265  }
266 
267  if( m_bNoColls && !rTextNd.HasSwAttrSet() )
268  return ;
269 
270  const SfxItemSet& rSet = rTextNd.GetSwAttrSet();
271 
272  SfxItemIter aIter( m_aComapeSet );
273  const SfxPoolItem* pItem = aIter.GetCurItem();
274  const SfxPoolItem* pFndItem;
275  sal_uInt16 nWhich;
276 
277  do
278  {
279  if( IsInvalidItem( pItem ) )
280  {
281  nWhich = m_aComapeSet.GetWhichByPos( aIter.GetCurPos() );
282  if( RES_TXTATR_END <= nWhich )
283  break; // end of text attributes
284 
285  if( SfxItemState::SET == rSet.GetItemState( nWhich, !m_bNoColls, &pFndItem )
286  && !CmpAttr( *pFndItem, rSet.GetPool()->GetDefaultItem( nWhich ) ))
287  {
288  m_pFindArr[ nWhich - m_nArrStart ] =
289  SwSrchChrAttr( *pFndItem, m_nNodeStart, m_nNodeEnd );
290  m_nFound++;
291  }
292  }
293  else
294  {
295  if( RES_TXTATR_END <= (nWhich = pItem->Which() ))
296  break; // end of text attributes
297 
298  if( CmpAttr( rSet.Get( nWhich, !m_bNoColls ), *pItem ) )
299  {
300  m_pFindArr[ nWhich - m_nArrStart ] =
301  SwSrchChrAttr( *pItem, m_nNodeStart, m_nNodeEnd );
302  m_nFound++;
303  }
304  }
305 
306  pItem = aIter.NextItem();
307  } while (pItem);
308 }
309 
310 static bool
311 lcl_IsAttributeIgnorable(sal_Int32 const nNdStart, sal_Int32 const nNdEnd,
312  SwSrchChrAttr const& rTmp)
313 {
314  // #i115528#: if there is a paragraph attribute, it has been added by the
315  // SwAttrCheckArr ctor, and nFound is 1.
316  // if the paragraph is entirely covered by hints that override the paragraph
317  // attribute, then this function must find an attribute to decrement nFound!
318  // so check for an empty search range, let attributes that start/end there
319  // cover it, and hope for the best...
320  return ((nNdEnd == nNdStart)
321  ? ((rTmp.nEnd < nNdStart) || (nNdEnd < rTmp.nStt))
322  : ((rTmp.nEnd <= nNdStart) || (nNdEnd <= rTmp.nStt)));
323 }
324 
325 bool SwAttrCheckArr::SetAttrFwd( const SwTextAttr& rAttr )
326 {
327  SwSrchChrAttr aTmp( rAttr.GetAttr(), rAttr.GetStart(), rAttr.GetAnyEnd() );
328 
329  // ignore all attributes not in search range
330  if (lcl_IsAttributeIgnorable(m_nNodeStart, m_nNodeEnd, aTmp))
331  {
332  return Found();
333  }
334 
335  const SfxPoolItem* pItem;
336  // here we explicitly also search in character templates
337  sal_uInt16 nWhch = rAttr.Which();
338  std::unique_ptr<SfxWhichIter> pIter;
339  const SfxPoolItem* pTmpItem = nullptr;
340  const SfxItemSet* pSet = nullptr;
341  if( RES_TXTATR_CHARFMT == nWhch || RES_TXTATR_AUTOFMT == nWhch )
342  {
343  if( m_bNoColls && RES_TXTATR_CHARFMT == nWhch )
344  return Found();
345  pTmpItem = nullptr;
346  pSet = CharFormat::GetItemSet( rAttr.GetAttr() );
347  if ( pSet )
348  {
349  pIter.reset(new SfxWhichIter( *pSet ));
350  nWhch = pIter->FirstWhich();
351  while( nWhch &&
352  SfxItemState::SET != pSet->GetItemState( nWhch, true, &pTmpItem ) )
353  nWhch = pIter->NextWhich();
354  if( !nWhch )
355  pTmpItem = nullptr;
356  }
357  }
358  else
359  pTmpItem = &rAttr.GetAttr();
360 
361  while( pTmpItem )
362  {
363  SfxItemState eState = m_aComapeSet.GetItemState( nWhch, false, &pItem );
364  if( SfxItemState::DONTCARE == eState || SfxItemState::SET == eState )
365  {
366  sal_uInt16 n;
367  SwSrchChrAttr* pCmp;
368 
369  // first delete all up to start position that are already invalid
370  SwSrchChrAttr* pArrPtr;
371  if( m_nFound )
372  for( pArrPtr = m_pFindArr, n = 0; n < m_nArrLen;
373  ++n, ++pArrPtr )
374  if( pArrPtr->nWhich && pArrPtr->nEnd <= aTmp.nStt )
375  {
376  pArrPtr->nWhich = 0; // deleted
377  m_nFound--;
378  }
379 
380  // delete all up to start position that are already invalid and
381  // move all "open" ones (= stick out over start position) from stack
382  // into FndSet
383  if( m_nStackCount )
384  for( pArrPtr = m_pStackArr, n=0; n < m_nArrLen; ++n, ++pArrPtr )
385  {
386  if( !pArrPtr->nWhich )
387  continue;
388 
389  if( pArrPtr->nEnd <= aTmp.nStt )
390  {
391  pArrPtr->nWhich = 0; // deleted
392  if( !--m_nStackCount )
393  break;
394  }
395  else if( pArrPtr->nStt <= aTmp.nStt )
396  {
397  pCmp = &m_pFindArr[ n ];
398  if( pCmp->nWhich )
399  {
400  if( pCmp->nEnd < pArrPtr->nEnd ) // extend
401  pCmp->nEnd = pArrPtr->nEnd;
402  }
403  else
404  {
405  *pCmp = *pArrPtr;
406  m_nFound++;
407  }
408  pArrPtr->nWhich = 0;
409  if( !--m_nStackCount )
410  break;
411  }
412  }
413 
414  bool bContinue = false;
415 
416  if( SfxItemState::DONTCARE == eState )
417  {
418  // Will the attribute become valid?
419  if( !CmpAttr( m_aComapeSet.GetPool()->GetDefaultItem( nWhch ),
420  *pTmpItem ))
421  {
422  // search attribute and extend if needed
423  pCmp = &m_pFindArr[ nWhch - m_nArrStart ];
424  if( !pCmp->nWhich )
425  {
426  *pCmp = aTmp; // not found, insert
427  m_nFound++;
428  }
429  else if( pCmp->nEnd < aTmp.nEnd ) // extend?
430  pCmp->nEnd = aTmp.nEnd;
431 
432  bContinue = true;
433  }
434  }
435  // Will the attribute become valid?
436  else if( CmpAttr( *pItem, *pTmpItem ) )
437  {
438  m_pFindArr[ nWhch - m_nArrStart ] = aTmp;
439  ++m_nFound;
440  bContinue = true;
441  }
442 
443  // then is has to go on the stack
444  if( !bContinue )
445  {
446  pCmp = &m_pFindArr[ nWhch - m_nArrStart ];
447  if (pCmp->nWhich )
448  {
449  // exists on stack, only if it is even bigger
450  if( pCmp->nEnd > aTmp.nEnd )
451  {
452  OSL_ENSURE( !m_pStackArr[ nWhch - m_nArrStart ].nWhich,
453  "slot on stack is still in use" );
454 
455  if( aTmp.nStt <= pCmp->nStt )
456  pCmp->nStt = aTmp.nEnd;
457  else
458  pCmp->nEnd = aTmp.nStt;
459 
460  m_pStackArr[ nWhch - m_nArrStart ] = *pCmp;
461  m_nStackCount++;
462  }
463  pCmp->nWhich = 0;
464  m_nFound--;
465  }
466  }
467  }
468  if( pIter )
469  {
470  assert(pSet && "otherwise no pIter");
471  nWhch = pIter->NextWhich();
472  while( nWhch &&
473  SfxItemState::SET != pSet->GetItemState( nWhch, true, &pTmpItem ) )
474  nWhch = pIter->NextWhich();
475  if( !nWhch )
476  break;
477  }
478  else
479  break;
480  }
481  pIter.reset();
482  return Found();
483 }
484 
485 bool SwAttrCheckArr::SetAttrBwd( const SwTextAttr& rAttr )
486 {
487  SwSrchChrAttr aTmp( rAttr.GetAttr(), rAttr.GetStart(), rAttr.GetAnyEnd() );
488 
489  // ignore all attributes not in search range
490  if (lcl_IsAttributeIgnorable(m_nNodeStart, m_nNodeEnd, aTmp))
491  {
492  return Found();
493  }
494 
495  const SfxPoolItem* pItem;
496  // here we explicitly also search in character templates
497  sal_uInt16 nWhch = rAttr.Which();
498  std::unique_ptr<SfxWhichIter> pIter;
499  const SfxPoolItem* pTmpItem = nullptr;
500  const SfxItemSet* pSet = nullptr;
501  if( RES_TXTATR_CHARFMT == nWhch || RES_TXTATR_AUTOFMT == nWhch )
502  {
503  if( m_bNoColls && RES_TXTATR_CHARFMT == nWhch )
504  return Found();
505 
506  pSet = CharFormat::GetItemSet( rAttr.GetAttr() );
507  if ( pSet )
508  {
509  pIter.reset( new SfxWhichIter( *pSet ) );
510  nWhch = pIter->FirstWhich();
511  while( nWhch &&
512  SfxItemState::SET != pSet->GetItemState( nWhch, true, &pTmpItem ) )
513  nWhch = pIter->NextWhich();
514  if( !nWhch )
515  pTmpItem = nullptr;
516  }
517  }
518  else
519  pTmpItem = &rAttr.GetAttr();
520 
521  while( pTmpItem )
522  {
523  SfxItemState eState = m_aComapeSet.GetItemState( nWhch, false, &pItem );
524  if( SfxItemState::DONTCARE == eState || SfxItemState::SET == eState )
525  {
526  sal_uInt16 n;
527  SwSrchChrAttr* pCmp;
528 
529  // first delete all up to start position that are already invalid
530  SwSrchChrAttr* pArrPtr;
531  if( m_nFound )
532  for( pArrPtr = m_pFindArr, n = 0; n < m_nArrLen; ++n, ++pArrPtr )
533  if( pArrPtr->nWhich && pArrPtr->nStt >= aTmp.nEnd )
534  {
535  pArrPtr->nWhich = 0; // deleted
536  m_nFound--;
537  }
538 
539  // delete all up to start position that are already invalid and
540  // move all "open" ones (= stick out over start position) from stack
541  // into FndSet
542  if( m_nStackCount )
543  for( pArrPtr = m_pStackArr, n = 0; n < m_nArrLen; ++n, ++pArrPtr )
544  {
545  if( !pArrPtr->nWhich )
546  continue;
547 
548  if( pArrPtr->nStt >= aTmp.nEnd )
549  {
550  pArrPtr->nWhich = 0; // deleted
551  if( !--m_nStackCount )
552  break;
553  }
554  else if( pArrPtr->nEnd >= aTmp.nEnd )
555  {
556  pCmp = &m_pFindArr[ n ];
557  if( pCmp->nWhich )
558  {
559  if( pCmp->nStt > pArrPtr->nStt ) // extend
560  pCmp->nStt = pArrPtr->nStt;
561  }
562  else
563  {
564  *pCmp = *pArrPtr;
565  m_nFound++;
566  }
567  pArrPtr->nWhich = 0;
568  if( !--m_nStackCount )
569  break;
570  }
571  }
572 
573  bool bContinue = false;
574  if( SfxItemState::DONTCARE == eState )
575  {
576  // Will the attribute become valid?
577  if( !CmpAttr( m_aComapeSet.GetPool()->GetDefaultItem( nWhch ),
578  *pTmpItem ) )
579  {
580  // search attribute and extend if needed
581  pCmp = &m_pFindArr[ nWhch - m_nArrStart ];
582  if( !pCmp->nWhich )
583  {
584  *pCmp = aTmp; // not found, insert
585  m_nFound++;
586  }
587  else if( pCmp->nStt > aTmp.nStt ) // extend?
588  pCmp->nStt = aTmp.nStt;
589 
590  bContinue = true;
591  }
592  }
593  // Will the attribute become valid?
594  else if( CmpAttr( *pItem, *pTmpItem ))
595  {
596  m_pFindArr[ nWhch - m_nArrStart ] = aTmp;
597  ++m_nFound;
598  bContinue = true;
599  }
600 
601  // then is has to go on the stack
602  if( !bContinue )
603  {
604  pCmp = &m_pFindArr[ nWhch - m_nArrStart ];
605  if( pCmp->nWhich )
606  {
607  // exists on stack, only if it is even bigger
608  if( pCmp->nStt < aTmp.nStt )
609  {
610  OSL_ENSURE( !m_pStackArr[ nWhch - m_nArrStart ].nWhich,
611  "slot on stack is still in use" );
612 
613  if( aTmp.nEnd <= pCmp->nEnd )
614  pCmp->nEnd = aTmp.nStt;
615  else
616  pCmp->nStt = aTmp.nEnd;
617 
618  m_pStackArr[ nWhch - m_nArrStart ] = *pCmp;
619  m_nStackCount++;
620  }
621  pCmp->nWhich = 0;
622  m_nFound--;
623  }
624  }
625  }
626  if( pIter )
627  {
628  assert(pSet && "otherwise no pIter");
629  nWhch = pIter->NextWhich();
630  while( nWhch &&
631  SfxItemState::SET != pSet->GetItemState( nWhch, true, &pTmpItem ) )
632  nWhch = pIter->NextWhich();
633  if( !nWhch )
634  break;
635  }
636  else
637  break;
638  }
639  pIter.reset();
640  return Found();
641 }
642 
643 sal_Int32 SwAttrCheckArr::Start() const
644 {
645  sal_Int32 nStart = m_nNodeStart;
646  SwSrchChrAttr* pArrPtr = m_pFindArr;
647  for( sal_uInt16 n = 0; n < m_nArrLen; ++n, ++pArrPtr )
648  if( pArrPtr->nWhich && pArrPtr->nStt > nStart )
649  nStart = pArrPtr->nStt;
650 
651  return nStart;
652 }
653 
654 sal_Int32 SwAttrCheckArr::End() const
655 {
656  SwSrchChrAttr* pArrPtr = m_pFindArr;
657  sal_Int32 nEnd = m_nNodeEnd;
658  for( sal_uInt16 n = 0; n < m_nArrLen; ++n, ++pArrPtr )
659  if( pArrPtr->nWhich && pArrPtr->nEnd < nEnd )
660  nEnd = pArrPtr->nEnd;
661 
662  return nEnd;
663 }
664 
665 bool SwAttrCheckArr::CheckStack()
666 {
667  if( !m_nStackCount )
668  return false;
669 
670  sal_uInt16 n;
671  const sal_Int32 nSttPos = Start();
672  const sal_Int32 nEndPos = End();
673  SwSrchChrAttr* pArrPtr;
674  for( pArrPtr = m_pStackArr, n = 0; n < m_nArrLen; ++n, ++pArrPtr )
675  {
676  if( !pArrPtr->nWhich )
677  continue;
678 
679  if( m_bForward ? pArrPtr->nEnd <= nSttPos : pArrPtr->nStt >= nEndPos )
680  {
681  pArrPtr->nWhich = 0; // deleted
682  if( !--m_nStackCount )
683  return m_nFound == m_aComapeSet.Count();
684  }
685  else if( m_bForward ? pArrPtr->nStt < nEndPos : pArrPtr->nEnd > nSttPos )
686  {
687  // move all "open" ones (= stick out over start position) into FndSet
688  OSL_ENSURE( !m_pFindArr[ n ].nWhich, "slot in array is already in use" );
689  m_pFindArr[ n ] = *pArrPtr;
690  pArrPtr->nWhich = 0;
691  m_nFound++;
692  if( !--m_nStackCount )
693  return m_nFound == m_aComapeSet.Count();
694  }
695  }
696  return m_nFound == m_aComapeSet.Count();
697 }
698 
699 static bool lcl_SearchForward( const SwTextNode& rTextNd, SwAttrCheckArr& rCmpArr,
700  SwPaM& rPam )
701 {
702  sal_Int32 nEndPos;
703  rCmpArr.SetNewSet( rTextNd, rPam );
704  if( !rTextNd.HasHints() )
705  {
706  if( !rCmpArr.Found() )
707  return false;
708  nEndPos = rCmpArr.GetNdEnd();
709  lcl_SetAttrPam( rPam, rCmpArr.GetNdStt(), &nEndPos, true );
710  return true;
711  }
712 
713  const SwpHints& rHtArr = rTextNd.GetSwpHints();
714  const SwTextAttr* pAttr;
715  size_t nPos = 0;
716 
717  // if everything is already there then check with which it will be ended
718  if( rCmpArr.Found() )
719  {
720  for( ; nPos < rHtArr.Count(); ++nPos )
721  {
722  pAttr = rHtArr.Get( nPos );
723  if( !rCmpArr.SetAttrFwd( *pAttr ) )
724  {
725  if( rCmpArr.GetNdStt() < pAttr->GetStart() )
726  {
727  // found end
728  auto nTmpStart = pAttr->GetStart();
729  lcl_SetAttrPam( rPam, rCmpArr.GetNdStt(),
730  &nTmpStart, true );
731  return true;
732  }
733  // continue search
734  break;
735  }
736  }
737 
738  if( nPos == rHtArr.Count() && rCmpArr.Found() )
739  {
740  // found
741  nEndPos = rCmpArr.GetNdEnd();
742  lcl_SetAttrPam( rPam, rCmpArr.GetNdStt(), &nEndPos, true );
743  return true;
744  }
745  }
746 
747  sal_Int32 nSttPos;
748  for( ; nPos < rHtArr.Count(); ++nPos )
749  {
750  pAttr = rHtArr.Get( nPos );
751  if( rCmpArr.SetAttrFwd( *pAttr ) )
752  {
753  // Do multiple start at that position? Do also check those:
754  nSttPos = pAttr->GetStart();
755  while( ++nPos < rHtArr.Count() )
756  {
757  pAttr = rHtArr.Get( nPos );
758  if( nSttPos != pAttr->GetStart() || !rCmpArr.SetAttrFwd( *pAttr ) )
759  break;
760  }
761 
762  if( !rCmpArr.Found() )
763  continue;
764 
765  // then we have our search area
766  if( (nSttPos = rCmpArr.Start()) > (nEndPos = rCmpArr.End()) )
767  return false;
768 
769  lcl_SetAttrPam( rPam, nSttPos, &nEndPos, true );
770  return true;
771  }
772  }
773 
774  if( !rCmpArr.CheckStack() )
775  return false;
776  nSttPos = rCmpArr.Start();
777  nEndPos = rCmpArr.End();
778  if( nSttPos > nEndPos )
779  return false;
780 
781  lcl_SetAttrPam( rPam, nSttPos, &nEndPos, true );
782  return true;
783 }
784 
785 static bool lcl_SearchBackward( const SwTextNode& rTextNd, SwAttrCheckArr& rCmpArr,
786  SwPaM& rPam )
787 {
788  sal_Int32 nEndPos;
789  rCmpArr.SetNewSet( rTextNd, rPam );
790  if( !rTextNd.HasHints() )
791  {
792  if( !rCmpArr.Found() )
793  return false;
794  nEndPos = rCmpArr.GetNdEnd();
795  lcl_SetAttrPam( rPam, rCmpArr.GetNdStt(), &nEndPos, false );
796  return true;
797  }
798 
799  const SwpHints& rHtArr = rTextNd.GetSwpHints();
800  const SwTextAttr* pAttr;
801  size_t nPos = rHtArr.Count();
802  sal_Int32 nSttPos;
803 
804  // if everything is already there then check with which it will be ended
805  if( rCmpArr.Found() )
806  {
807  while( nPos )
808  {
809  pAttr = rHtArr.GetSortedByEnd( --nPos );
810  if( !rCmpArr.SetAttrBwd( *pAttr ) )
811  {
812  nSttPos = pAttr->GetAnyEnd();
813  if( nSttPos < rCmpArr.GetNdEnd() )
814  {
815  // found end
816  nEndPos = rCmpArr.GetNdEnd();
817  lcl_SetAttrPam( rPam, nSttPos, &nEndPos, false );
818  return true;
819  }
820 
821  // continue search
822  break;
823  }
824  }
825 
826  if( !nPos && rCmpArr.Found() )
827  {
828  // found
829  nEndPos = rCmpArr.GetNdEnd();
830  lcl_SetAttrPam( rPam, rCmpArr.GetNdStt(), &nEndPos, false );
831  return true;
832  }
833  }
834 
835  while( nPos )
836  {
837  pAttr = rHtArr.GetSortedByEnd( --nPos );
838  if( rCmpArr.SetAttrBwd( *pAttr ) )
839  {
840  // Do multiple start at that position? Do also check those:
841  if( nPos )
842  {
843  nEndPos = pAttr->GetAnyEnd();
844  while( --nPos )
845  {
846  pAttr = rHtArr.GetSortedByEnd( nPos );
847  if( nEndPos != pAttr->GetAnyEnd() || !rCmpArr.SetAttrBwd( *pAttr ) )
848  break;
849  }
850  }
851  if( !rCmpArr.Found() )
852  continue;
853 
854  // then we have our search area
855  if( (nSttPos = rCmpArr.Start()) > (nEndPos = rCmpArr.End()) )
856  return false;
857 
858  lcl_SetAttrPam( rPam, nSttPos, &nEndPos, false );
859  return true;
860  }
861  }
862 
863  if( !rCmpArr.CheckStack() )
864  return false;
865  nSttPos = rCmpArr.Start();
866  nEndPos = rCmpArr.End();
867  if( nSttPos > nEndPos )
868  return false;
869 
870  lcl_SetAttrPam( rPam, nSttPos, &nEndPos, false );
871  return true;
872 }
873 
874 static bool lcl_Search( const SwContentNode& rCNd, const SfxItemSet& rCmpSet, bool bNoColls )
875 {
876  // search only hard attribution?
877  if( bNoColls && !rCNd.HasSwAttrSet() )
878  return false;
879 
880  const SfxItemSet& rNdSet = rCNd.GetSwAttrSet();
881  SfxItemIter aIter( rCmpSet );
882  const SfxPoolItem* pItem = aIter.GetCurItem();
883  const SfxPoolItem* pNdItem;
884  sal_uInt16 nWhich;
885 
886  do
887  {
888  if( IsInvalidItem( pItem ))
889  {
890  nWhich = rCmpSet.GetWhichByPos( aIter.GetCurPos() );
891  if( SfxItemState::SET != rNdSet.GetItemState( nWhich, !bNoColls, &pNdItem )
892  || CmpAttr( *pNdItem, rNdSet.GetPool()->GetDefaultItem( nWhich ) ))
893  return false;
894  }
895  else
896  {
897  nWhich = pItem->Which();
898 
899  if( !CmpAttr( rNdSet.Get( nWhich, !bNoColls ), *pItem ))
900  return false;
901  }
902 
903  pItem = aIter.NextItem();
904  } while (pItem);
905  return true; // found
906 }
907 
908 namespace sw {
909 
910 bool FindAttrImpl(SwPaM & rSearchPam,
911  const SfxPoolItem& rAttr, SwMoveFnCollection const & fnMove,
912  const SwPaM & rRegion, bool bInReadOnly,
913  SwRootFrame const*const pLayout)
914 {
915  // determine which attribute is searched:
916  const sal_uInt16 nWhich = rAttr.Which();
917  bool bCharAttr = isCHRATR(nWhich) || isTXTATR(nWhich);
918  assert(isTXTATR(nWhich)); // sw_redlinehide: only works for non-formatting hints such as needed in UpdateFields; use FindAttrsImpl for others
919 
920  std::unique_ptr<SwPaM> pPam(sw::MakeRegion(fnMove, rRegion));
921 
922  bool bFound = false;
923  bool bFirst = true;
924  const bool bSrchForward = &fnMove == &fnMoveForward;
925  SwContentNode * pNode;
926 
927  // if at beginning/end then move it out of the node
928  if( bSrchForward
929  ? pPam->GetPoint()->nContent.GetIndex() == pPam->GetContentNode()->Len()
930  : !pPam->GetPoint()->nContent.GetIndex() )
931  {
932  if( !(*fnMove.fnNds)( &pPam->GetPoint()->nNode, false ))
933  {
934  return false;
935  }
936  SwContentNode *pNd = pPam->GetContentNode();
937  pPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
938  }
939 
940  while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
941  {
942  if( bCharAttr )
943  {
944  if( !pNode->IsTextNode() ) // CharAttr are only in text nodes
945  continue;
946 
947  SwTextFrame const*const pFrame(pLayout
948  ? static_cast<SwTextFrame const*>(pNode->getLayoutFrame(pLayout))
949  : nullptr);
950  if (pFrame)
951  {
952  SwTextNode const* pAttrNode(nullptr);
953  SwTextAttr const* pAttr(nullptr);
954  if (bSrchForward)
955  {
956  sw::MergedAttrIter iter(*pFrame);
957  do
958  {
959  pAttr = iter.NextAttr(&pAttrNode);
960  }
961  while (pAttr
962  && (pAttrNode->GetIndex() < pPam->GetPoint()->nNode.GetIndex()
963  || (pAttrNode->GetIndex() == pPam->GetPoint()->nNode.GetIndex()
964  && pAttr->GetStart() < pPam->GetPoint()->nContent.GetIndex())
965  || pAttr->Which() != nWhich));
966  }
967  else
968  {
969  sw::MergedAttrIterReverse iter(*pFrame);
970  do
971  {
972  pAttr = iter.PrevAttr(&pAttrNode);
973  }
974  while (pAttr
975  && (pPam->GetPoint()->nNode.GetIndex() < pAttrNode->GetIndex()
976  || (pPam->GetPoint()->nNode.GetIndex() == pAttrNode->GetIndex()
977  && pPam->GetPoint()->nContent.GetIndex() <= pAttr->GetStart())
978  || pAttr->Which() != nWhich));
979  }
980  if (pAttr)
981  {
982  assert(pAttrNode);
983  pPam->GetPoint()->nNode = *pAttrNode;
984  lcl_SetAttrPam(*pPam, pAttr->GetStart(), pAttr->End(), bSrchForward);
985  bFound = true;
986  break;
987  }
988  }
989  else if (!pLayout && pNode->GetTextNode()->HasHints() &&
990  lcl_SearchAttr(*pNode->GetTextNode(), *pPam, rAttr, fnMove))
991  {
992  bFound = true;
993  }
994  if (bFound)
995  {
996  // set to the values of the attribute
997  rSearchPam.SetMark();
998  *rSearchPam.GetPoint() = *pPam->GetPoint();
999  *rSearchPam.GetMark() = *pPam->GetMark();
1000  break;
1001  }
1002  else if (isTXTATR(nWhich))
1003  continue;
1004  }
1005 
1006 #if 0
1007  // no hard attribution, so check if node was asked for this attr before
1008  if( !pNode->HasSwAttrSet() )
1009  {
1010  SwFormat* pTmpFormat = pNode->GetFormatColl();
1011  if( !aFormatArr.insert( pTmpFormat ).second )
1012  continue; // collection was requested earlier
1013  }
1014 
1015  if( SfxItemState::SET == pNode->GetSwAttrSet().GetItemState( nWhich,
1016  true, &pItem ))
1017  {
1018  // FORWARD: SPoint at the end, GetMark at the beginning of the node
1019  // BACKWARD: SPoint at the beginning, GetMark at the end of the node
1020  // always: incl. start and incl. end
1021  *rSearchPam.GetPoint() = *pPam->GetPoint();
1022  rSearchPam.SetMark();
1023  pNode->MakeEndIndex( &rSearchPam.GetPoint()->nContent );
1024  bFound = true;
1025  break;
1026  }
1027 #endif
1028  }
1029 
1030  // if backward search, switch point and mark
1031  if( bFound && !bSrchForward )
1032  rSearchPam.Exchange();
1033 
1034  return bFound;
1035 }
1036 
1037 } // namespace sw
1038 
1039 typedef bool (*FnSearchAttr)( const SwTextNode&, SwAttrCheckArr&, SwPaM& );
1040 
1041 static bool FindAttrsImpl(SwPaM & rSearchPam,
1042  const SfxItemSet& rSet, bool bNoColls, SwMoveFnCollection const & fnMove,
1043  const SwPaM & rRegion, bool bInReadOnly, bool bMoveFirst,
1044  SwRootFrame const*const pLayout)
1045 {
1046  std::unique_ptr<SwPaM> pPam(sw::MakeRegion(fnMove, rRegion));
1047 
1048  bool bFound = false;
1049  bool bFirst = true;
1050  const bool bSrchForward = &fnMove == &fnMoveForward;
1051  SwContentNode * pNode;
1052  SwpFormats aFormatArr;
1053 
1054  // check which text/char attributes are searched
1055  SwAttrCheckArr aCmpArr( rSet, bSrchForward, bNoColls );
1056  SfxItemSet aOtherSet( rSearchPam.GetDoc()->GetAttrPool(),
1058  aOtherSet.Put( rSet, false ); // got all invalid items
1059 
1060  FnSearchAttr fnSearch = bSrchForward
1061  ? (&::lcl_SearchForward)
1062  : (&::lcl_SearchBackward);
1063 
1064  // if at beginning/end then move it out of the node
1065  if( bMoveFirst &&
1066  ( bSrchForward
1067  ? pPam->GetPoint()->nContent.GetIndex() == pPam->GetContentNode()->Len()
1068  : !pPam->GetPoint()->nContent.GetIndex() ) )
1069  {
1070  if( !(*fnMove.fnNds)( &pPam->GetPoint()->nNode, false ))
1071  {
1072  return false;
1073  }
1074  SwContentNode *pNd = pPam->GetContentNode();
1075  pPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
1076  }
1077 
1078  while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
1079  {
1080  SwTextFrame const*const pFrame(pLayout && pNode->IsTextNode()
1081  ? static_cast<SwTextFrame const*>(pNode->getLayoutFrame(pLayout))
1082  : nullptr);
1083  assert(!pLayout || !pNode->IsTextNode() || pFrame);
1084  // sw_redlinehide: it's apparently not possible to find break items
1085  // with the UI, so checking one node is enough
1086  SwContentNode const& rPropsNode(*(pFrame
1087  ? pFrame->GetTextNodeForParaProps()
1088  : pNode));
1089 
1090  if( aCmpArr.Count() )
1091  {
1092  if( !pNode->IsTextNode() ) // CharAttr are only in text nodes
1093  continue;
1094 
1095  if (aOtherSet.Count() &&
1096  !lcl_Search(rPropsNode, aOtherSet, bNoColls))
1097  {
1098  continue;
1099  }
1100  sw::MergedPara const*const pMergedPara(pFrame ? pFrame->GetMergedPara() : nullptr);
1101  if (pMergedPara)
1102  {
1103  SwPosition const& rStart(*pPam->Start());
1104  SwPosition const& rEnd(*pPam->End());
1105  // no extents? fall back to searching index 0 of propsnode
1106  // to find its node items
1107  if (pMergedPara->extents.empty())
1108  {
1109  if (rStart.nNode.GetIndex() <= rPropsNode.GetIndex()
1110  && rPropsNode.GetIndex() <= rEnd.nNode.GetIndex())
1111  {
1112  SwPaM tmp(rPropsNode, 0, rPropsNode, 0);
1113  bFound = (*fnSearch)(*pNode->GetTextNode(), aCmpArr, tmp);
1114  if (bFound)
1115  {
1116  *pPam = tmp;
1117  }
1118  }
1119  }
1120  else
1121  {
1122  // iterate the extents, and intersect with input pPam:
1123  // the found ranges should never include delete redlines
1124  // so that subsequent Replace will not affect them
1125  for (size_t i = 0; i < pMergedPara->extents.size(); ++i)
1126  {
1127  auto const rExtent(pMergedPara->extents[bSrchForward
1128  ? i
1129  : pMergedPara->extents.size() - i - 1]);
1130  if (rExtent.pNode->GetIndex() < rStart.nNode.GetIndex()
1131  || rEnd.nNode.GetIndex() < rExtent.pNode->GetIndex())
1132  {
1133  continue;
1134  }
1135  sal_Int32 const nStart(rExtent.pNode == &rStart.nNode.GetNode()
1136  ? rStart.nContent.GetIndex()
1137  : 0);
1138  if (rExtent.nEnd <= nStart)
1139  {
1140  continue;
1141  }
1142  sal_Int32 const nEnd(rExtent.pNode == &rEnd.nNode.GetNode()
1143  ? rEnd.nContent.GetIndex()
1144  : rExtent.pNode->Len());
1145  if (nEnd < rExtent.nStart
1146  || (nStart != nEnd && nEnd == rExtent.nStart))
1147  {
1148  continue;
1149  }
1150  SwPaM tmp(*rExtent.pNode, std::max(nStart, rExtent.nStart),
1151  *rExtent.pNode, std::min(nEnd, rExtent.nEnd));
1152  tmp.Normalize(bSrchForward);
1153  bFound = (*fnSearch)(*rExtent.pNode, aCmpArr, tmp);
1154  if (bFound)
1155  {
1156  *pPam = tmp;
1157  break;
1158  }
1159  }
1160  }
1161  }
1162  else
1163  {
1164  bFound = (*fnSearch)(*pNode->GetTextNode(), aCmpArr, *pPam);
1165  }
1166  if (bFound)
1167  {
1168  // set to the values of the attribute
1169  rSearchPam.SetMark();
1170  *rSearchPam.GetPoint() = *pPam->GetPoint();
1171  *rSearchPam.GetMark() = *pPam->GetMark();
1172  break;
1173  }
1174  continue; // text attribute
1175  }
1176 
1177  if( !aOtherSet.Count() )
1178  continue;
1179 
1180  // no hard attribution, so check if node was asked for this attr before
1181  // (as an optimisation)
1182  if (!rPropsNode.HasSwAttrSet())
1183  {
1184  SwFormat* pTmpFormat = rPropsNode.GetFormatColl();
1185  if( !aFormatArr.insert( pTmpFormat ).second )
1186  continue; // collection was requested earlier
1187  }
1188 
1189  if (lcl_Search(rPropsNode, aOtherSet, bNoColls))
1190  {
1191  // FORWARD: SPoint at the end, GetMark at the beginning of the node
1192  // BACKWARD: SPoint at the beginning, GetMark at the end of the node
1193  if (pFrame)
1194  {
1195  *rSearchPam.GetPoint() = *pPam->GetPoint();
1196  rSearchPam.SetMark();
1197  *rSearchPam.GetMark() = pFrame->MapViewToModelPos(
1198  TextFrameIndex(bSrchForward ? pFrame->GetText().getLength() : 0));
1199  }
1200  else
1201  {
1202  *rSearchPam.GetPoint() = *pPam->GetPoint();
1203  rSearchPam.SetMark();
1204  if (bSrchForward)
1205  {
1206  pNode->MakeEndIndex( &rSearchPam.GetPoint()->nContent );
1207  }
1208  else
1209  {
1210  pNode->MakeStartIndex( &rSearchPam.GetPoint()->nContent );
1211  }
1212  }
1213  bFound = true;
1214  break;
1215  }
1216  }
1217 
1218  // in search direction, mark precedes point, because the next iteration
1219  // starts at point
1220  if (bFound)
1221  {
1222  rSearchPam.Normalize(!bSrchForward);
1223  }
1224 
1225  return bFound;
1226 }
1227 
1228 namespace {
1229 
1231 struct SwFindParaAttr : public SwFindParas
1232 {
1233  bool m_bNoCollection;
1234  const SfxItemSet *pSet, *pReplSet;
1235  const i18nutil::SearchOptions2 *pSearchOpt;
1236  SwCursor& m_rCursor;
1237  SwRootFrame const* m_pLayout;
1238  std::unique_ptr<utl::TextSearch> pSText;
1239 
1240  SwFindParaAttr( const SfxItemSet& rSet, bool bNoCollection,
1241  const i18nutil::SearchOptions2* pOpt, const SfxItemSet* pRSet,
1242  SwCursor& rCursor, SwRootFrame const*const pLayout)
1243  : m_bNoCollection(bNoCollection)
1244  , pSet( &rSet )
1245  , pReplSet( pRSet )
1246  , pSearchOpt( pOpt )
1247  , m_rCursor(rCursor)
1248  , m_pLayout(pLayout)
1249  {}
1250 
1251  virtual ~SwFindParaAttr() {}
1252 
1253  virtual int DoFind(SwPaM &, SwMoveFnCollection const &, const SwPaM &, bool bInReadOnly) override;
1254  virtual bool IsReplaceMode() const override;
1255 };
1256 
1257 }
1258 
1259 int SwFindParaAttr::DoFind(SwPaM & rCursor, SwMoveFnCollection const & fnMove,
1260  const SwPaM & rRegion, bool bInReadOnly)
1261 {
1262  // replace string (only if text given and search is not parameterized)?
1263  bool bReplaceText = pSearchOpt && ( !pSearchOpt->replaceString.isEmpty() ||
1264  !pSet->Count() );
1265  bool bReplaceAttr = pReplSet && pReplSet->Count();
1266  bool bMoveFirst = !bReplaceAttr;
1267  if( bInReadOnly && (bReplaceAttr || bReplaceText ))
1268  bInReadOnly = false;
1269 
1270  // We search for attributes, should we search for text as well?
1271  {
1272  SwPaM aRegion( *rRegion.GetMark(), *rRegion.GetPoint() );
1273  SwPaM* pTextRegion = &aRegion;
1274  SwPaM aSrchPam( *rCursor.GetPoint() );
1275 
1276  while( true )
1277  {
1278  if( pSet->Count() ) // any attributes?
1279  {
1280  // first attributes
1281  if (!FindAttrsImpl(aSrchPam, *pSet, m_bNoCollection, fnMove, aRegion, bInReadOnly, bMoveFirst, m_pLayout))
1282  return FIND_NOT_FOUND;
1283  bMoveFirst = true;
1284 
1285  if( !pSearchOpt )
1286  break; // ok, only attributes, so found
1287 
1288  pTextRegion = &aSrchPam;
1289  }
1290  else if( !pSearchOpt )
1291  return FIND_NOT_FOUND;
1292 
1293  // then search in text of it
1294  if( !pSText )
1295  {
1296  i18nutil::SearchOptions2 aTmp( *pSearchOpt );
1297 
1298  // search in selection
1299  aTmp.searchFlag |= (SearchFlags::REG_NOT_BEGINOFLINE |
1300  SearchFlags::REG_NOT_ENDOFLINE);
1301 
1302  aTmp.Locale = SvtSysLocale().GetLanguageTag().getLocale();
1303 
1304  pSText.reset( new utl::TextSearch( aTmp ) );
1305  }
1306 
1307  // TODO: searching for attributes in Outliner text?!
1308 
1309  // continue search in correct section (pTextRegion)
1310  if (sw::FindTextImpl(aSrchPam, *pSearchOpt, false/*bSearchInNotes*/, *pSText, fnMove, *pTextRegion, bInReadOnly, m_pLayout) &&
1311  *aSrchPam.GetMark() != *aSrchPam.GetPoint() )
1312  break; // found
1313  else if( !pSet->Count() )
1314  return FIND_NOT_FOUND; // only text and nothing found
1315 
1316  *aRegion.GetMark() = *aSrchPam.GetPoint();
1317  }
1318 
1319  *rCursor.GetPoint() = *aSrchPam.GetPoint();
1320  rCursor.SetMark();
1321  *rCursor.GetMark() = *aSrchPam.GetMark();
1322  }
1323 
1324  if( bReplaceText )
1325  {
1326  const bool bRegExp(
1327  SearchAlgorithms2::REGEXP == pSearchOpt->AlgorithmType2);
1328  SwIndex& rSttCntIdx = rCursor.Start()->nContent;
1329  const sal_Int32 nSttCnt = rSttCntIdx.GetIndex();
1330 
1331  // add to shell-cursor-ring so that the regions will be moved eventually
1332  SwPaM* pPrevRing(nullptr);
1333  if( bRegExp )
1334  {
1335  pPrevRing = const_cast<SwPaM &>(rRegion).GetPrev();
1336  const_cast<SwPaM &>(rRegion).GetRingContainer().merge( m_rCursor.GetRingContainer() );
1337  }
1338 
1339  std::optional<OUString> xRepl;
1340  if (bRegExp)
1341  xRepl = sw::ReplaceBackReferences(*pSearchOpt, &rCursor, m_pLayout);
1342  sw::ReplaceImpl(rCursor,
1343  xRepl ? *xRepl : pSearchOpt->replaceString, bRegExp,
1344  *m_rCursor.GetDoc(), m_pLayout);
1345 
1346  m_rCursor.SaveTableBoxContent( rCursor.GetPoint() );
1347 
1348  if( bRegExp )
1349  {
1350  // and remove region again
1351  SwPaM* p;
1352  SwPaM* pNext = const_cast<SwPaM*>(&rRegion);
1353  do {
1354  p = pNext;
1355  pNext = p->GetNext();
1356  p->MoveTo(const_cast<SwPaM*>(&rRegion));
1357  } while( p != pPrevRing );
1358  }
1359  rSttCntIdx = nSttCnt;
1360  }
1361 
1362  if( bReplaceAttr )
1363  {
1364  // is the selection still existent?
1365  // all searched attributes are reset to default if
1366  // they are not in ReplaceSet
1367  if( !pSet->Count() )
1368  {
1370  rCursor, *pReplSet, SetAttrMode::DEFAULT, m_pLayout);
1371  }
1372  else
1373  {
1374  SfxItemPool* pPool = pReplSet->GetPool();
1375  SfxItemSet aSet( *pPool, pReplSet->GetRanges() );
1376 
1377  SfxItemIter aIter( *pSet );
1378  const SfxPoolItem* pItem = aIter.GetCurItem();
1379  do
1380  {
1381  // reset all that are not set with pool defaults
1382  if( !IsInvalidItem( pItem ) && SfxItemState::SET !=
1383  pReplSet->GetItemState( pItem->Which(), false ))
1384  aSet.Put( pPool->GetDefaultItem( pItem->Which() ));
1385 
1386  pItem = aIter.NextItem();
1387  } while (pItem);
1388  aSet.Put( *pReplSet );
1390  rCursor, aSet, SetAttrMode::DEFAULT, m_pLayout);
1391  }
1392 
1393  return FIND_NO_RING;
1394  }
1395  else
1396  return FIND_FOUND;
1397 }
1398 
1399 bool SwFindParaAttr::IsReplaceMode() const
1400 {
1401  return ( pSearchOpt && !pSearchOpt->replaceString.isEmpty() ) ||
1402  ( pReplSet && pReplSet->Count() );
1403 }
1404 
1406 sal_uLong SwCursor::FindAttrs( const SfxItemSet& rSet, bool bNoCollections,
1407  SwDocPositions nStart, SwDocPositions nEnd,
1408  bool& bCancel, FindRanges eFndRngs,
1409  const i18nutil::SearchOptions2* pSearchOpt,
1410  const SfxItemSet* pReplSet,
1411  SwRootFrame const*const pLayout)
1412 {
1413  // switch off OLE-notifications
1414  SwDoc* pDoc = GetDoc();
1415  Link<bool,void> aLnk( pDoc->GetOle2Link() );
1416  pDoc->SetOle2Link( Link<bool,void>() );
1417 
1418  bool bReplace = ( pSearchOpt && ( !pSearchOpt->replaceString.isEmpty() ||
1419  !rSet.Count() ) ) ||
1420  (pReplSet && pReplSet->Count());
1421  bool const bStartUndo = pDoc->GetIDocumentUndoRedo().DoesUndo() && bReplace;
1422  if (bStartUndo)
1423  {
1424  pDoc->GetIDocumentUndoRedo().StartUndo( SwUndoId::REPLACE, nullptr );
1425  }
1426 
1427  SwFindParaAttr aSwFindParaAttr( rSet, bNoCollections, pSearchOpt,
1428  pReplSet, *this, pLayout );
1429 
1430  sal_uLong nRet = FindAll( aSwFindParaAttr, nStart, nEnd, eFndRngs, bCancel );
1431  pDoc->SetOle2Link( aLnk );
1432  if( nRet && bReplace )
1433  pDoc->getIDocumentState().SetModified();
1434 
1435  if (bStartUndo)
1436  {
1437  pDoc->GetIDocumentUndoRedo().EndUndo( SwUndoId::REPLACE, nullptr );
1438  }
1439 
1440  return nRet;
1441 }
1442 
1443 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::optional< OUString > ReplaceBackReferences(const i18nutil::SearchOptions2 &rSearchOpt, SwPaM *const pPam, SwRootFrame const *const pLayout)
Helperfunction to resolve backward references in regular expressions.
Definition: findtxt.cxx:1098
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:151
virtual sal_Int32 Len() const
Definition: node.cxx:1181
constexpr TypedWhichId< SwFormatPageDesc > RES_PAGEDESC(93)
sal_uLong GetIndex() const
Definition: node.hxx:282
Marks a position in the document model.
Definition: pam.hxx:35
SwContentNode * GetNode(SwPaM &rPam, bool &rbFirst, SwMoveFnCollection const &fnMove, bool const bInReadOnly, SwRootFrame const *const i_pLayout)
This function returns the next node in direction of search.
Definition: pam.cxx:816
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
bool isCHRATR(const sal_uInt16 nWhich)
Definition: hintids.hxx:471
const OUString & GetText() const
Definition: ndtxt.hxx:211
std::unique_ptr< SwPaM > MakeRegion(SwMoveFnCollection const &fnMove, const SwPaM &rOrigRg)
make a new region
Definition: pam.cxx:521
std::string GetValue
bool isTXTATR(const sal_uInt16 nWhich)
Definition: hintids.hxx:485
const SwTextAttr * GetBkwrdTextHint(const SwpHints &rHtsArr, size_t &rPos, sal_Int32 nContentPos)
Definition: findattr.cxx:93
SwNodeIndex nNode
Definition: pam.hxx:37
virtual void InsertItemSet(const SwPaM &rRg, const SfxItemSet &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr)=0
virtual void SetModified()=0
Must be called manually at changes of format.
sal_uIntPtr sal_uLong
const SwPosition * GetMark() const
Definition: pam.hxx:209
sw::MergedPara * GetMergedPara()
Definition: txtfrm.hxx:442
SwContentFrame * getLayoutFrame(const SwRootFrame *, const SwPosition *pPos=nullptr, std::pair< Point, bool > const *pViewPosAndCalcFrame=nullptr) const
Definition: node.cxx:1148
sal_Int64 n
virtual SwUndoId EndUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Closes undo block.
Definition: doc.hxx:186
void SetOle2Link(const Link< bool, void > &rLink)
Definition: doc.hxx:1326
SwTextAttr const * PrevAttr(SwTextNode const **ppNode=nullptr)
Definition: txtfrm.cxx:222
static bool lcl_IsAttributeIgnorable(sal_Int32 const nNdStart, sal_Int32 const nNdEnd, SwSrchChrAttr const &rTmp)
Definition: findattr.cxx:311
SwTextAttr * GetSortedByEnd(size_t nPos) const
Definition: ndhints.hxx:158
Dialog to specify the properties of date form field.
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
sal_uInt16 Which() const
Definition: txatbase.hxx:110
const OUString & GetText() const
Returns the text portion we want to edit (for inline see underneath)
Definition: txtfrm.cxx:1280
sal_Int32 GetAnyEnd() const
end (if available), else start
Definition: txatbase.hxx:153
static bool CmpAttr(const SfxPoolItem &rItem1, const SfxPoolItem &rItem2)
Definition: findattr.cxx:55
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:314
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_FONT(7)
void MoveTo(value_type *pDestRing)
Removes this item from its current ring container and adds it to another ring container.
Definition: ring.hxx:135
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
SwTextAttr const * NextAttr(SwTextNode const **ppNode=nullptr)
Definition: txtfrm.cxx:85
SwDocPositions
Definition: cshtyp.hxx:103
SwContentNode * GetContentNode(bool bPoint=true) const
Definition: pam.hxx:229
const SfxPoolItem * NextItem()
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)
Search.
Definition: findtxt.cxx:359
constexpr sal_uInt16 RES_PARATR_BEGIN(RES_TXTATR_END)
SwIndex nContent
Definition: pam.hxx:38
const Link< bool, void > & GetOle2Link() const
Definition: doc.hxx:1327
GetHint fnGetHint
Definition: pamtyp.hxx:76
FindRanges
Definition: cshtyp.hxx:90
sal_Int32 GetStart() const
Definition: txatbase.hxx:82
constexpr TypedWhichId< SwFormatCharFormat > RES_TXTATR_CHARFMT(52)
Describes parts of multiple text nodes, which will form a text frame, even when redlines are hidden a...
Definition: txtfrm.hxx:960
constexpr TypedWhichId< SwFormatAutoFormat > RES_TXTATR_AUTOFMT(50)
virtual bool DoesUndo() const =0
Is Undo enabled?
SwPaM * GetNext()
Definition: pam.hxx:264
void Normalize(bool bPointFirst=true)
Normalizes PaM, i.e.
Definition: pam.cxx:538
bool FindAttrImpl(SwPaM &rSearchPam, const SfxPoolItem &rAttr, SwMoveFnCollection const &fnMove, const SwPaM &rRegion, bool bInReadOnly, SwRootFrame const *const pLayout)
Definition: findattr.cxx:910
Base class for various Writer styles.
Definition: format.hxx:43
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
SwTextNode const * GetTextNodeForParaProps() const
Definition: txtfrm.cxx:1290
virtual bool IsReplaceMode() const =0
size_t Count() const
Definition: ndhints.hxx:142
virtual SwUndoId StartUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Opens undo block.
sal_uLong FindAll(SwFindParas &, SwDocPositions, SwDocPositions, FindRanges, bool &bCancel)
Definition: swcrsr.cxx:943
SwTextAttr * Get(size_t nPos) const
Definition: ndhints.hxx:144
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
int i
const SwPosition * GetPoint() const
Definition: pam.hxx:207
const SwTextAttr * GetFrwrdTextHint(const SwpHints &rHtsArr, size_t &rPos, sal_Int32 nContentPos)
Definition: findattr.cxx:80
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:206
static bool lcl_Search(const SwContentNode &rCNd, const SfxItemSet &rCmpSet, bool bNoColls)
Definition: findattr.cxx:874
Count
void Exchange()
Definition: pam.cxx:489
SwContentNode * GetContentNode()
Definition: node.hxx:615
sal_uInt16 Count() const
void MakeStartIndex(SwIndex *pIdx)
Definition: node.hxx:391
SwDoc * GetDoc() const
Definition: pam.hxx:243
Marks a character position inside a document model node.
Definition: index.hxx:37
const SfxItemSet * GetItemSet(const SfxPoolItem &rAttr)
Returns the item set associated with a character/inet/auto style.
Definition: atrstck.cxx:133
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:393
constexpr sal_uInt16 RES_GRFATR_END(148)
SwpHints & GetSwpHints()
getters for SwpHints
Definition: ndtxt.hxx:802
bool HasSwAttrSet() const
Definition: node.hxx:444
sal_uInt16 GetWhichByPos(sal_uInt16 nPos) const
bool ReplaceImpl(SwPaM &rCursor, OUString const &rReplacement, bool const bRegExp, SwDoc &rDoc, SwRootFrame const *const pLayout)
Definition: findtxt.cxx:1030
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
SfxItemPool * GetPool() const
const int FIND_NO_RING
Definition: swcrsr.hxx:36
const SwPosition * Start() const
Definition: pam.hxx:212
sal_uInt16 GetCurPos() const
constexpr TypedWhichId< SvxColorItem > RES_CHRATR_COLOR(3)
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:80
constexpr sal_uInt16 RES_TXTATR_END(RES_TXTATR_NOEND_END)
An SwTextAttr container, stores all directly formatted text portions for a text node.
Definition: ndhints.hxx:67
SfxItemState
constexpr sal_uInt16 RES_CHRATR_BEGIN(HINT_BEGIN)
virtual int DoFind(SwPaM &, SwMoveFnCollection const &, const SwPaM &, bool)=0
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
sal_uLong FindAttrs(const SfxItemSet &rSet, bool bNoCollections, SwDocPositions nStart, SwDocPositions nEnd, bool &bCancel, FindRanges, const i18nutil::SearchOptions2 *pSearchOpt, const SfxItemSet *rReplSet=nullptr, SwRootFrame const *const pLayout=nullptr)
search for attributes
Definition: findattr.cxx:1406
SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:59
o3tl::sorted_vector< SwFormat * > SwpFormats
Definition: findattr.cxx:52
const int FIND_FOUND
Definition: swcrsr.hxx:35
sal_Int32 GetIndex() const
Definition: index.hxx:95
const sal_Int32 * End() const
Definition: txatbase.hxx:148
void * p
static bool FindAttrsImpl(SwPaM &rSearchPam, const SfxItemSet &rSet, bool bNoColls, SwMoveFnCollection const &fnMove, const SwPaM &rRegion, bool bInReadOnly, bool bMoveFirst, SwRootFrame const *const pLayout)
Definition: findattr.cxx:1041
const SwAttrSet & GetSwAttrSet() const
Does node has already its own auto-attributes? Access to SwAttrSet.
Definition: node.hxx:723
const SfxPoolItem & GetAttr() const
Definition: txatbase.hxx:159
const LanguageTag & GetLanguageTag() const
SwFormatColl * GetFormatColl() const
Definition: node.hxx:447
const int FIND_NOT_FOUND
Definition: swcrsr.hxx:34
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:475
std::pair< const_iterator, bool > insert(Value &&x)
bool IsInvalidItem(const SfxPoolItem *pItem)
o3tl::strong_int< sal_Int32, struct Tag_TextFrameIndex > TextFrameIndex
Denotes a character index in a text frame at a layout level, after extent mapping from a text node at...
bool IsTextNode() const
Definition: node.hxx:636
SwPosition MapViewToModelPos(TextFrameIndex nIndex) const
Definition: txtfrm.cxx:1235
Found
bool(* FnSearchAttr)(const SwTextNode &, SwAttrCheckArr &, SwPaM &)
Definition: findattr.cxx:1039
static void lcl_SetAttrPam(SwPaM &rPam, sal_Int32 nStart, const sal_Int32 *pEnd, const bool bSaveMark)
Definition: findattr.cxx:106
void MakeEndIndex(SwIndex *pIdx)
Definition: node.hxx:392
sal_uInt16 Which() const
static bool lcl_SearchForward(const SwTextNode &rTextNd, SwAttrCheckArr &rCmpArr, SwPaM &rPam)
Definition: findattr.cxx:699
sal_uInt16 nPos
const SfxPoolItem * GetCurItem() const
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1317
static bool lcl_SearchAttr(const SwTextNode &rTextNd, SwPaM &rPam, const SfxPoolItem &rCmpItem, SwMoveFnCollection const &fnMove)
search for a text attribute
Definition: findattr.cxx:143
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:836
bool HasHints() const
Definition: ndtxt.hxx:221
static bool lcl_SearchBackward(const SwTextNode &rTextNd, SwAttrCheckArr &rCmpArr, SwPaM &rPam)
Definition: findattr.cxx:785