update emoji autocorrect entries from po-files
[LibreOffice.git] / sc / source / core / tool / reffind.cxx
blob94db172b2447fa2306dc9e4d0142e1240f3824df
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "reffind.hxx"
21 #include "global.hxx"
22 #include "compiler.hxx"
23 #include "document.hxx"
25 // STATIC DATA
26 namespace {
28 // Include colon; addresses in range reference are handled individually.
29 const sal_Unicode pDelimiters[] = {
30 '=','(',')','+','-','*','/','^','&',' ','{','}','<','>',':', 0
33 inline bool IsText( sal_Unicode c )
35 bool bFound = ScGlobal::UnicodeStrChr( pDelimiters, c );
36 if (bFound)
37 // This is one of delimiters, therefore not text.
38 return false;
40 // argument separator is configurable.
41 const sal_Unicode sep = ScCompiler::GetNativeSymbolChar(ocSep);
42 return c != sep;
45 inline bool IsText( bool& bQuote, sal_Unicode c )
47 if (c == '\'')
49 bQuote = !bQuote;
50 return true;
52 if (bQuote)
53 return true;
55 return IsText(c);
58 /**
59 * Find first character position that is considered text. A character is
60 * considered a text when it's within the ascii range and when it's not a
61 * delimiter.
63 sal_Int32 FindStartPos(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
65 while (nStartPos <= nEndPos && !IsText(p[nStartPos]))
66 ++nStartPos;
68 return nStartPos;
71 sal_Int32 FindEndPosA1(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
73 bool bQuote = false;
74 sal_Int32 nNewEnd = nStartPos;
75 while (nNewEnd <= nEndPos && IsText(bQuote, p[nNewEnd]))
76 ++nNewEnd;
78 return nNewEnd;
81 sal_Int32 FindEndPosR1C1(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos)
83 sal_Int32 nNewEnd = nStartPos;
84 p = &p[nStartPos];
85 for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
87 if (*p == '\'')
89 // Skip until the closing quote.
90 for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
91 if (*p == '\'')
92 break;
93 if (nNewEnd > nEndPos)
94 break;
96 else if (*p == '[')
98 // Skip until the closing braket.
99 for (; nNewEnd <= nEndPos; ++p, ++nNewEnd)
100 if (*p == ']')
101 break;
102 if (nNewEnd > nEndPos)
103 break;
105 else if (!IsText(*p))
106 break;
109 return nNewEnd;
113 * Find last character position that is considred text, from the specified
114 * start position.
116 sal_Int32 FindEndPos(const sal_Unicode* p, sal_Int32 nStartPos, sal_Int32 nEndPos,
117 formula::FormulaGrammar::AddressConvention eConv)
119 switch (eConv)
121 case formula::FormulaGrammar::CONV_XL_R1C1:
122 return FindEndPosR1C1(p, nStartPos, nEndPos);
123 case formula::FormulaGrammar::CONV_OOO:
124 case formula::FormulaGrammar::CONV_XL_A1:
125 default:
126 return FindEndPosA1(p, nStartPos, nEndPos);
130 void ExpandToTextA1(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos)
132 bool bQuote = false; // skip quoted text
133 while (rStartPos > 0 && IsText(bQuote, p[rStartPos - 1]) )
134 --rStartPos;
135 if (rEndPos)
136 --rEndPos;
137 while (rEndPos+1 < nLen && IsText(p[rEndPos + 1]) )
138 ++rEndPos;
141 void ExpandToTextR1C1(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos)
143 // move back the start position to the first text character.
144 if (rStartPos > 0)
146 for (--rStartPos; rStartPos > 0; --rStartPos)
148 sal_Unicode c = p[rStartPos];
149 if (c == '\'')
151 // Skip until the opening quote.
152 for (--rStartPos; rStartPos > 0; --rStartPos)
154 c = p[rStartPos];
155 if (c == '\'')
156 break;
158 if (rStartPos == 0)
159 break;
161 else if (c == ']')
163 // Skip until the opening braket.
164 for (--rStartPos; rStartPos > 0; --rStartPos)
166 c = p[rStartPos];
167 if (c == '[')
168 break;
170 if (rStartPos == 0)
171 break;
173 else if (!IsText(c))
175 ++rStartPos;
176 break;
181 // move forward the end position to the last text character.
182 rEndPos = FindEndPosR1C1(p, rEndPos, nLen-1);
185 void ExpandToText(const sal_Unicode* p, sal_Int32 nLen, sal_Int32& rStartPos, sal_Int32& rEndPos,
186 formula::FormulaGrammar::AddressConvention eConv)
188 switch (eConv)
190 case formula::FormulaGrammar::CONV_XL_R1C1:
191 ExpandToTextR1C1(p, nLen, rStartPos, rEndPos);
192 break;
193 case formula::FormulaGrammar::CONV_OOO:
194 case formula::FormulaGrammar::CONV_XL_A1:
195 default:
196 ExpandToTextA1(p, nLen, rStartPos, rEndPos);
202 ScRefFinder::ScRefFinder(
203 const OUString& rFormula, const ScAddress& rPos,
204 ScDocument* pDoc, formula::FormulaGrammar::AddressConvention eConvP) :
205 maFormula(rFormula),
206 meConv(eConvP),
207 mpDoc(pDoc),
208 maPos(rPos),
209 mnFound(0),
210 mnSelStart(0),
211 mnSelEnd(0)
215 ScRefFinder::~ScRefFinder()
219 static sal_uInt16 lcl_NextFlags( sal_uInt16 nOld )
221 sal_uInt16 nNew = nOld & 7; // die drei Abs-Flags
222 nNew = ( nNew - 1 ) & 7; // weiterzaehlen
224 if (!(nOld & SCA_TAB_3D))
225 nNew &= ~SCA_TAB_ABSOLUTE; // not 3D -> never absolute!
227 return ( nOld & 0xfff8 ) | nNew;
230 void ScRefFinder::ToggleRel( sal_Int32 nStartPos, sal_Int32 nEndPos )
232 sal_Int32 nLen = maFormula.getLength();
233 if (nLen <= 0)
234 return;
235 const sal_Unicode* pSource = maFormula.getStr(); // for quick access
237 // expand selection, and instead of selection start- and end-index
239 if ( nEndPos < nStartPos )
240 ::std::swap(nEndPos, nStartPos);
242 ExpandToText(pSource, nLen, nStartPos, nEndPos, meConv);
244 OUString aResult;
245 OUString aExpr;
246 OUString aSep;
247 ScAddress aAddr;
248 mnFound = 0;
250 sal_Int32 nLoopStart = nStartPos;
251 while ( nLoopStart <= nEndPos )
253 // Determine the start and end positions of a text segment. Note that
254 // the end position returned from FindEndPos may be one position after
255 // the last character position in case of the last segment.
256 sal_Int32 nEStart = FindStartPos(pSource, nLoopStart, nEndPos);
257 sal_Int32 nEEnd = FindEndPos(pSource, nEStart, nEndPos, meConv);
259 aSep = maFormula.copy(nLoopStart, nEStart-nLoopStart);
260 if (nEEnd < maFormula.getLength())
261 aExpr = maFormula.copy(nEStart, nEEnd-nEStart);
262 else
263 aExpr = maFormula.copy(nEStart);
265 // Check the validity of the expression, and toggle the relative flag.
266 ScAddress::Details aDetails(meConv, maPos.Row(), maPos.Col());
267 ScAddress::ExternalInfo aExtInfo;
268 sal_uInt16 nResult = aAddr.Parse(aExpr, mpDoc, aDetails, &aExtInfo);
269 if ( nResult & SCA_VALID )
271 sal_uInt16 nFlags = lcl_NextFlags( nResult );
272 if( aExtInfo.mbExternal )
273 { // retain external doc name and tab name before toggle relative flag
274 sal_Int32 nSep;
275 switch(meConv)
277 case formula::FormulaGrammar::CONV_XL_A1 :
278 case formula::FormulaGrammar::CONV_XL_OOX :
279 case formula::FormulaGrammar::CONV_XL_R1C1 :
280 nSep = aExpr.lastIndexOf('!');
281 break;
282 case formula::FormulaGrammar::CONV_OOO :
283 default:
284 nSep = aExpr.lastIndexOf('.');
285 break;
287 if (nSep < 0)
289 assert(!"Invalid syntax according to address convention.");
291 else
293 OUString aRef = aExpr.copy(nSep+1);
294 OUString aExtDocNameTabName = aExpr.copy(0, nSep+1);
295 nResult = aAddr.Parse(aRef, mpDoc, aDetails);
296 aAddr.SetTab(0); // force to first tab to avoid error on checking
297 nFlags = lcl_NextFlags( nResult );
298 aExpr = aExtDocNameTabName + aAddr.Format(nFlags, mpDoc, aDetails);
301 else
303 aExpr = aAddr.Format(nFlags, mpDoc, aDetails);
306 sal_Int32 nAbsStart = nStartPos+aResult.getLength()+aSep.getLength();
308 if (!mnFound) // first reference ?
309 mnSelStart = nAbsStart;
310 mnSelEnd = nAbsStart + aExpr.getLength(); // selection, no indices
311 ++mnFound;
314 // assemble
316 aResult += aSep;
317 aResult += aExpr;
319 nLoopStart = nEEnd;
322 OUString aTotal = maFormula.copy(0, nStartPos);
323 aTotal += aResult;
324 if (nEndPos < maFormula.getLength()-1)
325 aTotal += maFormula.copy(nEndPos+1);
327 maFormula = aTotal;
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */