1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <UndoOverwrite.hxx>
21 #include <unotools/charclass.hxx>
22 #include <unotools/transliterationwrapper.hxx>
23 #include <comphelper/processfactory.hxx>
25 #include <IDocumentUndoRedo.hxx>
26 #include <IDocumentRedlineAccess.hxx>
27 #include <IShellCursorSupplier.hxx>
32 #include <UndoCore.hxx>
34 #include <acorrect.hxx>
36 #include <strings.hrc>
39 using namespace ::com::sun::star
;
40 using namespace ::com::sun::star::i18n
;
41 using namespace ::com::sun::star::uno
;
43 SwUndoOverwrite::SwUndoOverwrite( SwDoc
& rDoc
, SwPosition
& rPos
,
45 : SwUndo(SwUndoId::OVERWRITE
, &rDoc
),
48 SwTextNode
*const pTextNd
= rPos
.GetNode().GetTextNode();
50 sal_Int32
const nTextNdLen
= pTextNd
->GetText().getLength();
52 m_nStartNode
= rPos
.GetNodeIndex();
53 m_nStartContent
= rPos
.GetContentIndex();
55 if( !rDoc
.getIDocumentRedlineAccess().IsIgnoreRedline() && !rDoc
.getIDocumentRedlineAccess().GetRedlineTable().empty() )
57 SwPaM
aPam( rPos
.GetNode(), rPos
.GetContentIndex(),
58 rPos
.GetNode(), rPos
.GetContentIndex()+1 );
59 m_pRedlSaveData
.reset( new SwRedlineSaveDatas
);
60 if( !FillSaveData( aPam
, *m_pRedlSaveData
, false ))
62 m_pRedlSaveData
.reset();
64 if (m_nStartContent
< nTextNdLen
)
66 rDoc
.getIDocumentRedlineAccess().DeleteRedline(aPam
, false, RedlineType::Any
);
71 if( m_nStartContent
< nTextNdLen
) // no pure insert?
73 m_aDelStr
+= OUStringChar( pTextNd
->GetText()[m_nStartContent
] );
75 m_pHistory
.reset( new SwHistory
);
76 SwRegHistory
aRHst( *pTextNd
, m_pHistory
.get() );
77 m_pHistory
->CopyAttr( pTextNd
->GetpSwpHints(), m_nStartNode
, 0,
79 rPos
.AdjustContent(+1);
83 bool bOldExpFlg
= pTextNd
->IsIgnoreDontExpand();
84 pTextNd
->SetIgnoreDontExpand( true );
86 pTextNd
->InsertText( OUString(cIns
), rPos
, SwInsertFlags::EMPTYEXPAND
);
87 m_aInsStr
+= OUStringChar( cIns
);
91 const SwContentIndex
aTmpIndex( rPos
.GetContentNode(), rPos
.GetContentIndex() - 2 );
92 pTextNd
->EraseText( aTmpIndex
, 1 );
94 pTextNd
->SetIgnoreDontExpand( bOldExpFlg
);
96 m_bCacheComment
= false;
99 SwUndoOverwrite::~SwUndoOverwrite()
103 bool SwUndoOverwrite::CanGrouping( SwDoc
& rDoc
, SwPosition
& rPos
,
106 // What is with only inserted characters?
108 // Only deletion of single chars can be combined.
109 if( rPos
.GetNodeIndex() != m_nStartNode
|| m_aInsStr
.isEmpty() ||
110 ( !m_bGroup
&& m_aInsStr
.getLength() != 1 ))
113 // Is the node a TextNode at all?
114 SwTextNode
* pDelTextNd
= rPos
.GetNode().GetTextNode();
116 (pDelTextNd
->GetText().getLength() != rPos
.GetContentIndex() &&
117 rPos
.GetContentIndex() != ( m_nStartContent
+ m_aInsStr
.getLength() )))
120 CharClass
& rCC
= GetAppCharClass();
122 // ask the char that should be inserted
123 if (( CH_TXTATR_BREAKWORD
== cIns
|| CH_TXTATR_INWORD
== cIns
) ||
124 rCC
.isLetterNumeric( OUString( cIns
), 0 ) !=
125 rCC
.isLetterNumeric( m_aInsStr
, m_aInsStr
.getLength()-1 ) )
128 if (!m_bInsChar
&& rPos
.GetContentIndex() < pDelTextNd
->GetText().getLength())
130 SwRedlineSaveDatas aTmpSav
;
131 SwPaM
aPam( rPos
.GetNode(), rPos
.GetContentIndex(),
132 rPos
.GetNode(), rPos
.GetContentIndex()+1 );
134 const bool bSaved
= FillSaveData( aPam
, aTmpSav
, false );
136 bool bOk
= ( !m_pRedlSaveData
&& !bSaved
) ||
137 ( m_pRedlSaveData
&& bSaved
&&
138 SwUndo::CanRedlineGroup( *m_pRedlSaveData
, aTmpSav
,
139 m_nStartContent
> rPos
.GetContentIndex() ));
140 // aTmpSav.DeleteAndDestroyAll();
144 rDoc
.getIDocumentRedlineAccess().DeleteRedline( aPam
, false, RedlineType::Any
);
147 // both 'overwrites' can be combined so 'move' the corresponding character
150 if (rPos
.GetContentIndex() < pDelTextNd
->GetText().getLength())
152 m_aDelStr
+= OUStringChar( pDelTextNd
->GetText()[rPos
.GetContentIndex()] );
153 rPos
.AdjustContent(+1);
159 bool bOldExpFlg
= pDelTextNd
->IsIgnoreDontExpand();
160 pDelTextNd
->SetIgnoreDontExpand( true );
162 OUString
const ins( pDelTextNd
->InsertText(OUString(cIns
), rPos
,
163 SwInsertFlags::EMPTYEXPAND
) );
164 assert(ins
.getLength() == 1); // check in SwDoc::Overwrite => cannot fail
166 m_aInsStr
+= OUStringChar( cIns
);
170 const SwContentIndex
aTmpIndex( rPos
.GetContentNode(), rPos
.GetContentIndex() - 2 );
171 pDelTextNd
->EraseText( aTmpIndex
, 1 );
173 pDelTextNd
->SetIgnoreDontExpand( bOldExpFlg
);
179 void SwUndoOverwrite::UndoImpl(::sw::UndoRedoContext
& rContext
)
181 SwDoc
& rDoc
= rContext
.GetDoc();
182 SwCursor
& rCurrentPam(rContext
.GetCursorSupplier().CreateNewShellCursor());
184 rCurrentPam
.DeleteMark();
185 rCurrentPam
.GetPoint()->Assign( m_nStartNode
);
186 SwTextNode
* pTextNd
= rCurrentPam
.GetPointNode().GetTextNode();
188 SwPosition
& rPtPos
= *rCurrentPam
.GetPoint();
189 rPtPos
.SetContent( m_nStartContent
);
191 SwAutoCorrExceptWord
* pACEWord
= rDoc
.GetAutoCorrExceptWord();
194 if( 1 == m_aInsStr
.getLength() && 1 == m_aDelStr
.getLength() )
195 pACEWord
->CheckChar( *rCurrentPam
.GetPoint(), m_aDelStr
[0] );
196 rDoc
.SetAutoCorrExceptWord( nullptr );
199 // If there was not only an overwrite but also an insert, delete the surplus
200 if( m_aInsStr
.getLength() > m_aDelStr
.getLength() )
202 rPtPos
.AdjustContent( m_aDelStr
.getLength() );
203 pTextNd
->EraseText( rPtPos
, m_aInsStr
.getLength() - m_aDelStr
.getLength() );
204 rPtPos
.SetContent( m_nStartContent
);
207 if( !m_aDelStr
.isEmpty() )
209 bool bOldExpFlg
= pTextNd
->IsIgnoreDontExpand();
210 pTextNd
->SetIgnoreDontExpand( true );
212 rPtPos
.AdjustContent(+1);
213 for( sal_Int32 n
= 0; n
< m_aDelStr
.getLength(); n
++ )
215 // do it individually, to keep the attributes!
216 OUString
aTmpStr(m_aDelStr
[n
]);
217 OUString
const ins( pTextNd
->InsertText(aTmpStr
, rPtPos
) );
218 assert(ins
.getLength() == 1); // cannot fail
220 rPtPos
.AdjustContent(-2);
221 pTextNd
->EraseText( rPtPos
, 1 );
222 rPtPos
.AdjustContent(+2);
224 pTextNd
->SetIgnoreDontExpand( bOldExpFlg
);
225 rPtPos
.AdjustContent(-1);
230 if( pTextNd
->GetpSwpHints() )
231 pTextNd
->ClearSwpHintsArr( false );
232 m_pHistory
->TmpRollback( &rDoc
, 0, false );
235 if( rCurrentPam
.GetMark()->GetContentIndex() != m_nStartContent
)
237 rCurrentPam
.SetMark();
238 rCurrentPam
.GetMark()->SetContent( m_nStartContent
);
241 if( m_pRedlSaveData
)
242 SetSaveData( rDoc
, *m_pRedlSaveData
);
245 void SwUndoOverwrite::RepeatImpl(::sw::RepeatContext
& rContext
)
247 SwPaM
& rCurrentPam
= rContext
.GetRepeatPaM();
248 if( m_aInsStr
.isEmpty() || rCurrentPam
.HasMark() )
251 SwDoc
& rDoc
= rContext
.GetDoc();
254 ::sw::GroupUndoGuard
const undoGuard(rDoc
.GetIDocumentUndoRedo());
255 rDoc
.getIDocumentContentOperations().Overwrite(rCurrentPam
, OUString(m_aInsStr
[0]));
257 for( sal_Int32 n
= 1; n
< m_aInsStr
.getLength(); ++n
)
258 rDoc
.getIDocumentContentOperations().Overwrite(rCurrentPam
, OUString(m_aInsStr
[n
]));
261 void SwUndoOverwrite::RedoImpl(::sw::UndoRedoContext
& rContext
)
263 SwDoc
& rDoc
= rContext
.GetDoc();
264 SwCursor
& rCurrentPam(rContext
.GetCursorSupplier().CreateNewShellCursor());
266 rCurrentPam
.DeleteMark();
267 rCurrentPam
.GetPoint()->Assign(m_nStartNode
);
268 SwTextNode
* pTextNd
= rCurrentPam
.GetPointNode().GetTextNode();
270 SwPosition
& rPtPos
= *rCurrentPam
.GetPoint();
272 if( m_pRedlSaveData
)
274 rPtPos
.SetContent( m_nStartContent
);
275 rCurrentPam
.SetMark();
276 rCurrentPam
.GetMark()->AdjustContent( m_aDelStr
.getLength() );
277 rDoc
.getIDocumentRedlineAccess().DeleteRedline( rCurrentPam
, false, RedlineType::Any
);
278 rCurrentPam
.DeleteMark();
280 rPtPos
.SetContent( !m_aDelStr
.isEmpty() ? m_nStartContent
+1 : m_nStartContent
);
282 bool bOldExpFlg
= pTextNd
->IsIgnoreDontExpand();
283 pTextNd
->SetIgnoreDontExpand( true );
285 for( sal_Int32 n
= 0; n
< m_aInsStr
.getLength(); n
++ )
287 // do it individually, to keep the attributes!
289 pTextNd
->InsertText( OUString(m_aInsStr
[n
]), rPtPos
,
290 SwInsertFlags::EMPTYEXPAND
) );
291 assert(ins
.getLength() == 1); // cannot fail
293 if( n
< m_aDelStr
.getLength() )
295 rPtPos
.AdjustContent(-2);
296 pTextNd
->EraseText( rPtPos
, 1 );
297 rPtPos
.AdjustContent( n
+1 < m_aDelStr
.getLength() ? 2 : 1 );
300 pTextNd
->SetIgnoreDontExpand( bOldExpFlg
);
302 // get back old start position from UndoNodes array
304 m_pHistory
->SetTmpEnd( m_pHistory
->Count() );
305 if( rCurrentPam
.GetMark()->GetContentIndex() != m_nStartContent
)
307 rCurrentPam
.SetMark();
308 rCurrentPam
.GetMark()->SetContent( m_nStartContent
);
312 SwRewriter
SwUndoOverwrite::GetRewriter() const
316 OUString aString
= SwResId(STR_START_QUOTE
) +
317 ShortenString(m_aInsStr
, nUndoStringLength
, SwResId(STR_LDOTS
)) +
318 SwResId(STR_END_QUOTE
);
320 aResult
.AddRule(UndoArg1
, aString
);
325 struct UndoTransliterate_Data
328 std::unique_ptr
<SwHistory
> pHistory
;
329 std::optional
<Sequence
< sal_Int32
>> oOffsets
;
331 sal_Int32 nStart
, nLen
;
333 UndoTransliterate_Data( SwNodeOffset nNd
, sal_Int32 nStt
, sal_Int32 nStrLen
, OUString aText
)
334 : sText(std::move( aText
)),
335 nNdIdx( nNd
), nStart( nStt
), nLen( nStrLen
)
338 void SetChangeAtNode( SwDoc
& rDoc
);
341 SwUndoTransliterate::SwUndoTransliterate(
343 const utl::TransliterationWrapper
& rTrans
)
344 : SwUndo( SwUndoId::TRANSLITERATE
, &rPam
.GetDoc() ), SwUndRng( rPam
), m_nType( rTrans
.getType() )
348 SwUndoTransliterate::~SwUndoTransliterate()
352 void SwUndoTransliterate::UndoImpl(::sw::UndoRedoContext
& rContext
)
354 SwDoc
& rDoc
= rContext
.GetDoc();
356 // since the changes were added to the vector from the end of the string/node towards
357 // the start, we need to revert them from the start towards the end now to keep the
358 // offset information of the undo data in sync with the changing text.
359 // Thus we need to iterate from the end of the vector to the start
360 for (sal_Int32 i
= m_aChanges
.size() - 1; i
>= 0; --i
)
361 m_aChanges
[i
]->SetChangeAtNode( rDoc
);
363 AddUndoRedoPaM(rContext
, true);
366 void SwUndoTransliterate::RedoImpl(::sw::UndoRedoContext
& rContext
)
368 SwPaM
& rPam( AddUndoRedoPaM(rContext
) );
369 DoTransliterate(rContext
.GetDoc(), rPam
);
372 void SwUndoTransliterate::RepeatImpl(::sw::RepeatContext
& rContext
)
374 DoTransliterate(rContext
.GetDoc(), rContext
.GetRepeatPaM());
377 void SwUndoTransliterate::DoTransliterate(SwDoc
& rDoc
, SwPaM
const & rPam
)
379 utl::TransliterationWrapper
aTrans( ::comphelper::getProcessComponentContext(), m_nType
);
380 rDoc
.getIDocumentContentOperations().TransliterateText( rPam
, aTrans
);
383 void SwUndoTransliterate::AddChanges( SwTextNode
& rTNd
,
384 sal_Int32 nStart
, sal_Int32 nLen
,
385 uno::Sequence
<sal_Int32
> const & rOffsets
)
387 tools::Long nOffsLen
= rOffsets
.getLength();
388 UndoTransliterate_Data
* pNew
= new UndoTransliterate_Data(
389 rTNd
.GetIndex(), nStart
, static_cast<sal_Int32
>(nOffsLen
),
390 rTNd
.GetText().copy(nStart
, nLen
));
392 m_aChanges
.push_back( std::unique_ptr
<UndoTransliterate_Data
>(pNew
) );
394 const sal_Int32
* pOffsets
= rOffsets
.getConstArray();
395 // where did we need less memory ?
396 const sal_Int32
* p
= pOffsets
;
397 for( tools::Long n
= 0; n
< nOffsLen
; ++n
, ++p
)
398 if( *p
!= ( nStart
+ n
))
400 // create the Offset array
401 pNew
->oOffsets
.emplace( nLen
);
402 sal_Int32
* pIdx
= pNew
->oOffsets
->getArray();
404 tools::Long nMyOff
, nNewVal
= nStart
;
405 for( n
= 0, nMyOff
= nStart
; n
< nOffsLen
; ++p
, ++n
, ++nMyOff
)
409 // something is deleted
411 *(pIdx
-1) = nNewVal
++;
413 else if( *p
> nMyOff
)
415 for( ; *p
> nMyOff
; ++nMyOff
)
425 // and then we need to save the attributes/bookmarks
426 // but this data must moved every time to the last in the chain!
427 for (size_t i
= 0; i
+ 1 < m_aChanges
.size(); ++i
) // check all changes but not the current one
429 UndoTransliterate_Data
* pD
= m_aChanges
[i
].get();
430 if( pD
->nNdIdx
== pNew
->nNdIdx
&& pD
->pHistory
)
432 // same node and have a history?
433 pNew
->pHistory
= std::move(pD
->pHistory
);
434 break; // more can't exist
438 if( !pNew
->pHistory
)
440 pNew
->pHistory
.reset( new SwHistory
);
441 SwRegHistory
aRHst( rTNd
, pNew
->pHistory
.get() );
442 pNew
->pHistory
->CopyAttr( rTNd
.GetpSwpHints(),
443 pNew
->nNdIdx
, 0, rTNd
.GetText().getLength(), false );
449 void UndoTransliterate_Data::SetChangeAtNode( SwDoc
& rDoc
)
451 SwTextNode
* pTNd
= rDoc
.GetNodes()[ nNdIdx
]->GetTextNode();
455 Sequence
<sal_Int32
> aOffsets( oOffsets
? oOffsets
->getLength() : nLen
);
457 aOffsets
= *oOffsets
;
460 sal_Int32
* p
= aOffsets
.getArray();
461 for( sal_Int32 n
= 0; n
< nLen
; ++n
, ++p
)
464 pTNd
->ReplaceTextOnly( nStart
, nLen
, sText
, aOffsets
);
468 if( pTNd
->GetpSwpHints() )
469 pTNd
->ClearSwpHintsArr( false );
470 pHistory
->TmpRollback( &rDoc
, 0, false );
471 pHistory
->SetTmpEnd( pHistory
->Count() );
475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */