[Libreoffice-commits] core.git: 17 commits - formula/source include/formula sc/inc sc/qa sc/source

Kohei Yoshida kohei.yoshida at collabora.com
Mon Nov 18 14:24:25 PST 2013


 formula/source/core/api/FormulaCompiler.cxx |    6 
 include/formula/FormulaCompiler.hxx         |    3 
 sc/inc/compiler.hxx                         |   30 +-
 sc/inc/externalrefmgr.hxx                   |    7 
 sc/inc/tokenstringcontext.hxx               |   18 +
 sc/qa/unit/ucalc.cxx                        |    2 
 sc/qa/unit/ucalc_formula.cxx                |   98 +++++++
 sc/source/core/data/documen3.cxx            |    2 
 sc/source/core/tool/compiler.cxx            |  358 ++++++++++------------------
 sc/source/core/tool/formulagroup.cxx        |    2 
 sc/source/core/tool/token.cxx               |  236 +++++++++++++++---
 sc/source/core/tool/tokenstringcontext.cxx  |   83 ++++++
 sc/source/filter/oox/formulabuffer.cxx      |   12 
 sc/source/ui/docshell/externalrefmgr.cxx    |   14 +
 14 files changed, 570 insertions(+), 301 deletions(-)

New commits:
commit eeb91d8f57424c3e2c3d2ea371dd8d00af287953
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Mon Nov 18 15:26:05 2013 -0500

    Handle external reference bits too. This is the last missing piece.
    
    Change-Id: Ib1dd18bb5a8d70c53722ac91e2340d05bbc7798a

diff --git a/sc/inc/externalrefmgr.hxx b/sc/inc/externalrefmgr.hxx
index c072cfd..d5b4d7f 100644
--- a/sc/inc/externalrefmgr.hxx
+++ b/sc/inc/externalrefmgr.hxx
@@ -586,6 +586,13 @@ public:
      * @return const OUString* external document URI.
      */
     const OUString* getExternalFileName(sal_uInt16 nFileId, bool bForceOriginal = false);
+
+    /**
+     * Get all cached external file names as an array. Array indices of the
+     * returned name array correspond with external file ID's.
+     */
+    std::vector<OUString> getAllCachedExternalFileNames() const;
+
     bool hasExternalFile(sal_uInt16 nFileId) const;
     bool hasExternalFile(const OUString& rFile) const;
     const SrcFileData* getExternalFileData(sal_uInt16 nFileId) const;
diff --git a/sc/inc/tokenstringcontext.hxx b/sc/inc/tokenstringcontext.hxx
index 7af90eb..85b61f7 100644
--- a/sc/inc/tokenstringcontext.hxx
+++ b/sc/inc/tokenstringcontext.hxx
@@ -27,6 +27,7 @@ namespace sc {
 struct SC_DLLPUBLIC TokenStringContext
 {
     typedef boost::unordered_map<sal_uInt16, OUString> IndexNameMapType;
+    typedef boost::unordered_map<size_t, std::vector<OUString> > IndexNamesMapType;
     typedef boost::unordered_map<SCTAB, IndexNameMapType> TabIndexMapType;
 
     formula::FormulaGrammar::Grammar meGram;
@@ -39,6 +40,9 @@ struct SC_DLLPUBLIC TokenStringContext
     TabIndexMapType maSheetRangeNames;
     IndexNameMapType maNamedDBs;
 
+    std::vector<OUString> maExternalFileNames;
+    IndexNamesMapType maExternalCachedTabNames;
+
     TokenStringContext( const ScDocument* pDoc, formula::FormulaGrammar::Grammar eGram );
 };
 
diff --git a/sc/qa/unit/ucalc_formula.cxx b/sc/qa/unit/ucalc_formula.cxx
index 7ae2554..6db441c 100644
--- a/sc/qa/unit/ucalc_formula.cxx
+++ b/sc/qa/unit/ucalc_formula.cxx
@@ -115,11 +115,21 @@ void Test::testFormulaCreateStringFromTokens()
         "SUM(sheetx;x;y;z)", // sheet local and global named ranges mixed
         "MAX(Table1)+MIN(Table2)*SUM(Table3)", // database ranges
         "{1;TRUE;3|FALSE;5;\"Text\"|;;}", // inline matrix
+        "SUM('file:///path/to/fake.file'#$Sheet.A1:B10)",
     };
 
     boost::scoped_ptr<ScTokenArray> pArray;
 
     sc::TokenStringContext aCxt(m_pDoc, formula::FormulaGrammar::GRAM_ENGLISH);
+
+    // Artificially add external refererence data after the context object is
+    // initialized.
+    aCxt.maExternalFileNames.push_back("file:///path/to/fake.file");
+    std::vector<OUString> aExtTabNames;
+    aExtTabNames.push_back("Sheet");
+    aCxt.maExternalCachedTabNames.insert(
+        sc::TokenStringContext::IndexNamesMapType::value_type(0, aExtTabNames));
+
     ScAddress aPos(0,0,0);
 
     for (size_t i = 0, n = SAL_N_ELEMENTS(aTests); i < n; ++i)
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index fcd2aea..f74c5ee 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -3221,7 +3221,40 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
 {
     if (rToken.IsExternalRef())
     {
-        // TODO : Implement this.
+        size_t nFileId = rToken.GetIndex();
+        OUString aTabName = rToken.GetString().getString();
+        if (nFileId >= rCxt.maExternalFileNames.size())
+            // out of bound
+            return;
+
+        OUString aFileName = rCxt.maExternalFileNames[nFileId];
+
+        switch (rToken.GetType())
+        {
+            case svExternalName:
+                rBuf.append(rCxt.mpRefConv->makeExternalNameStr(aFileName, aTabName));
+            break;
+            case svExternalSingleRef:
+                rCxt.mpRefConv->makeExternalRefStr(
+                       rBuf, rPos, aFileName, aTabName, static_cast<const ScToken&>(rToken).GetSingleRef());
+            break;
+            case svExternalDoubleRef:
+            {
+                sc::TokenStringContext::IndexNamesMapType::const_iterator it =
+                    rCxt.maExternalCachedTabNames.find(nFileId);
+
+                if (it == rCxt.maExternalCachedTabNames.end())
+                    return;
+
+                rCxt.mpRefConv->makeExternalRefStr(
+                    rBuf, rPos, aFileName, it->second, aTabName, static_cast<const ScToken&>(rToken).GetDoubleRef());
+            }
+            break;
+            default:
+                // warning, not error, otherwise we may end up with a never
+                // ending message box loop if this was the cursor cell to be redrawn.
+                OSL_FAIL("appendTokenByType: unknown type of ocExternalRef");
+        }
         return;
     }
 
diff --git a/sc/source/core/tool/tokenstringcontext.cxx b/sc/source/core/tool/tokenstringcontext.cxx
index e3aea0e..a68ae75 100644
--- a/sc/source/core/tool/tokenstringcontext.cxx
+++ b/sc/source/core/tool/tokenstringcontext.cxx
@@ -11,6 +11,7 @@
 #include "compiler.hxx"
 #include "document.hxx"
 #include "dbdata.hxx"
+#include "externalrefmgr.hxx"
 
 using namespace com::sun::star;
 
@@ -86,6 +87,22 @@ TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::Formula
             maNamedDBs.insert(IndexNameMapType::value_type(rData.GetIndex(), rData.GetName()));
         }
     }
+
+    // Fetch all relevant bits for external references.
+    if (pDoc->HasExternalRefManager())
+    {
+        const ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
+        maExternalFileNames = pRefMgr->getAllCachedExternalFileNames();
+        for (size_t i = 0, n = maExternalFileNames.size(); i < n; ++i)
+        {
+            sal_uInt16 nFileId = static_cast<sal_uInt16>(i);
+            std::vector<OUString> aTabNames;
+            pRefMgr->getAllCachedTableNames(nFileId, aTabNames);
+            if (!aTabNames.empty())
+                maExternalCachedTabNames.insert(
+                    IndexNamesMapType::value_type(nFileId, aTabNames));
+        }
+    }
 }
 
 }
diff --git a/sc/source/ui/docshell/externalrefmgr.cxx b/sc/source/ui/docshell/externalrefmgr.cxx
index c06490e..4646103 100644
--- a/sc/source/ui/docshell/externalrefmgr.cxx
+++ b/sc/source/ui/docshell/externalrefmgr.cxx
@@ -2480,6 +2480,20 @@ const OUString* ScExternalRefManager::getExternalFileName(sal_uInt16 nFileId, bo
     return &maSrcFiles[nFileId].maFileName;
 }
 
+std::vector<OUString> ScExternalRefManager::getAllCachedExternalFileNames() const
+{
+    std::vector<OUString> aNames;
+    aNames.reserve(maSrcFiles.size());
+    std::vector<SrcFileData>::const_iterator it = maSrcFiles.begin(), itEnd = maSrcFiles.end();
+    for (; it != itEnd; ++it)
+    {
+        const SrcFileData& rData = *it;
+        aNames.push_back(rData.maFileName);
+    }
+
+    return aNames;
+}
+
 bool ScExternalRefManager::hasExternalFile(sal_uInt16 nFileId) const
 {
     return nFileId < maSrcFiles.size();
commit 6adf0ba4c8815c95b9bdbf274c0e2f774aed9a9f
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Mon Nov 18 14:39:13 2013 -0500

    Avoid passing the external ref manager pointer to make it re-entrant.
    
    Change-Id: I67a0df1dcd0635ea82bfc397041a9c43dedbe75d

diff --git a/sc/inc/compiler.hxx b/sc/inc/compiler.hxx
index b6251f7..a8757e7 100644
--- a/sc/inc/compiler.hxx
+++ b/sc/inc/compiler.hxx
@@ -252,13 +252,14 @@ public:
 
         virtual OUString makeExternalNameStr( const OUString& rFile, const OUString& rName ) const = 0;
 
-        virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                         sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
-                                         ScExternalRefManager* pRefMgr ) const = 0;
-
-        virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                         sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
-                                         ScExternalRefManager* pRefMgr ) const = 0;
+        virtual void makeExternalRefStr(
+            OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+            const OUString& rTabName, const ScSingleRefData& rRef ) const = 0;
+
+        virtual void makeExternalRefStr(
+            OUStringBuffer& rBuffer, const ScAddress& rPos,
+            const OUString& rFileName, const std::vector<OUString>& rTabNames,
+            const OUString& rTabName, const ScComplexRefData& rRef ) const = 0;
 
         enum SpecialSymbolType
         {
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index d3e696e..1668973 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -821,22 +821,17 @@ struct ConventionOOO_A1 : public Convention_A1
     }
 
     bool makeExternalSingleRefStr(
-        OUStringBuffer& rBuffer, sal_uInt16 nFileId,
-        const OUString& rTabName, const ScSingleRefData& rRef, const ScAddress& rPos,
-        ScExternalRefManager* pRefMgr, bool bDisplayTabName, bool bEncodeUrl ) const
+        OUStringBuffer& rBuffer, const OUString& rFileName, const OUString& rTabName,
+        const ScSingleRefData& rRef, const ScAddress& rPos, bool bDisplayTabName, bool bEncodeUrl ) const
     {
         ScAddress aAbsRef = rRef.toAbs(rPos);
         if (bDisplayTabName)
         {
             OUString aFile;
-            const OUString* p = pRefMgr->getExternalFileName(nFileId);
-            if (p)
-            {
-                if (bEncodeUrl)
-                    aFile = *p;
-                else
-                    aFile = INetURLObject::decode(*p, INET_HEX_ESCAPE, INetURLObject::DECODE_UNAMBIGUOUS);
-            }
+            if (bEncodeUrl)
+                aFile = rFileName;
+            else
+                aFile = INetURLObject::decode(rFileName, INET_HEX_ESCAPE, INetURLObject::DECODE_UNAMBIGUOUS);
 
             rBuffer.append("'" + aFile.replaceAll("'", "''") + "'#");
 
@@ -857,29 +852,30 @@ struct ConventionOOO_A1 : public Convention_A1
         return true;
     }
 
-    void makeExternalRefStrImpl( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
-                                     ScExternalRefManager* pRefMgr, bool bODF ) const
+    void makeExternalRefStrImpl(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const OUString& rTabName, const ScSingleRefData& rRef, bool bODF ) const
     {
         if (bODF)
             rBuffer.append( '[');
 
         bool bEncodeUrl = bODF;
-        makeExternalSingleRefStr(rBuffer, nFileId, rTabName, rRef, rPos, pRefMgr, true, bEncodeUrl);
+        makeExternalSingleRefStr(rBuffer, rFileName, rTabName, rRef, rPos, true, bEncodeUrl);
         if (bODF)
             rBuffer.append( ']');
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const OUString& rTabName, const ScSingleRefData& rRef ) const
     {
-        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, false);
+        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabName, rRef, false);
     }
 
-    void makeExternalRefStrImpl( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
-                                     ScExternalRefManager* pRefMgr, bool bODF ) const
+    void makeExternalRefStrImpl(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const std::vector<OUString>& rTabNames, const OUString& rTabName,
+        const ScComplexRefData& rRef, bool bODF ) const
     {
         ScRange aAbsRange = rRef.toAbs(rPos);
 
@@ -890,7 +886,7 @@ struct ConventionOOO_A1 : public Convention_A1
 
         do
         {
-            if (!makeExternalSingleRefStr(rBuffer, nFileId, rTabName, rRef.Ref1, rPos, pRefMgr, true, bEncodeUrl))
+            if (!makeExternalSingleRefStr(rBuffer, rFileName, rTabName, rRef.Ref1, rPos, true, bEncodeUrl))
                 break;
 
             rBuffer.append(':');
@@ -900,14 +896,7 @@ struct ConventionOOO_A1 : public Convention_A1
             if (bDisplayTabName)
             {
                 // Get the name of the last table.
-                vector<OUString> aTabNames;
-                pRefMgr->getAllCachedTableNames(nFileId, aTabNames);
-                if (aTabNames.empty())
-                {
-                    OSL_TRACE( "ConventionOOO_A1::makeExternalRefStrImpl: no sheet names for document ID %d", nFileId);
-                }
-
-                if (!lcl_getLastTabName(aLastTabName, rTabName, aTabNames, aAbsRange))
+                if (!lcl_getLastTabName(aLastTabName, rTabName, rTabNames, aAbsRange))
                 {
                     OSL_FAIL( "ConventionOOO_A1::makeExternalRefStrImpl: sheet name not found");
                     // aLastTabName contains #REF!, proceed.
@@ -915,18 +904,20 @@ struct ConventionOOO_A1 : public Convention_A1
             }
             else if (bODF)
                 rBuffer.append( '.');      // need at least the sheet separator in ODF
-            makeExternalSingleRefStr( rBuffer, nFileId, aLastTabName,
-                    rRef.Ref2, rPos, pRefMgr, bDisplayTabName, bEncodeUrl);
+            makeExternalSingleRefStr(
+                rBuffer, rFileName, aLastTabName, rRef.Ref2, rPos, bDisplayTabName, bEncodeUrl);
         } while (0);
+
         if (bODF)
             rBuffer.append( ']');
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const std::vector<OUString>& rTabNames, const OUString& rTabName,
+        const ScComplexRefData& rRef ) const
     {
-        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, false);
+        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabNames, rTabName, rRef, false);
     }
 };
 
@@ -975,18 +966,19 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
         return lcl_makeExternalNameStr( rFile, rName, '#', true);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const OUString& rTabName, const ScSingleRefData& rRef ) const
     {
-        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, true);
+        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabName, rRef, true);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const std::vector<OUString>& rTabNames,
+        const OUString& rTabName, const ScComplexRefData& rRef ) const
     {
-        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, true);
+        makeExternalRefStrImpl(rBuffer, rPos, rFileName, rTabNames, rTabName, rRef, true);
     }
 };
 
@@ -1277,9 +1269,9 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         return ConventionXL::makeExternalNameStr(rFile, rName);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const OUString& rTabName, const ScSingleRefData& rRef ) const
     {
         // ['file:///path/to/file/filename.xls']'Sheet Name'!$A$1
         // This is a little different from the format Excel uses, as Excel
@@ -1287,34 +1279,22 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         // whole file path with [] because the file name can contain any
         // characters.
 
-        const OUString* pFullName = pRefMgr->getExternalFileName(nFileId);
-        if (!pFullName)
-            return;
-
-        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
+        ConventionXL::makeExternalDocStr(rBuffer, rFileName, false);
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
         rBuffer.append('!');
 
         makeSingleCellStr(rBuffer, rRef, rRef.toAbs(rPos));
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const std::vector<OUString>& rTabNames, const OUString& rTabName,
+        const ScComplexRefData& rRef ) const
     {
-        const OUString* pFullName = pRefMgr->getExternalFileName(nFileId);
-        if (!pFullName)
-            return;
-
-        vector<OUString> aTabNames;
-        pRefMgr->getAllCachedTableNames(nFileId, aTabNames);
-        if (aTabNames.empty())
-            return;
-
         ScRange aAbsRef = rRef.toAbs(rPos);
 
-        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
-        ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, aTabNames, aAbsRef);
+        ConventionXL::makeExternalDocStr(rBuffer, rFileName, false);
+        ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, rTabNames, aAbsRef);
         rBuffer.append('!');
 
         makeSingleCellStr(rBuffer, rRef.Ref1, aAbsRef.aStart);
@@ -1470,9 +1450,9 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         return ConventionXL::makeExternalNameStr(rFile, rName);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const OUString& rTabName, const ScSingleRefData& rRef ) const
     {
         // ['file:///path/to/file/filename.xls']'Sheet Name'!$A$1
         // This is a little different from the format Excel uses, as Excel
@@ -1480,13 +1460,8 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         // whole file path with [] because the file name can contain any
         // characters.
 
-        const OUString* pFullName = pRefMgr->getExternalFileName(nFileId);
-        if (!pFullName)
-            return;
-
         ScAddress aAbsRef = rRef.toAbs(rPos);
-
-        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
+        ConventionXL::makeExternalDocStr(rBuffer, rFileName, false);
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
         rBuffer.append('!');
 
@@ -1494,23 +1469,15 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         r1c1_add_col(rBuffer, rRef, aAbsRef);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
-                                     sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
-                                     ScExternalRefManager* pRefMgr ) const
+    virtual void makeExternalRefStr(
+        OUStringBuffer& rBuffer, const ScAddress& rPos, const OUString& rFileName,
+        const std::vector<OUString>& rTabNames, const OUString& rTabName,
+        const ScComplexRefData& rRef ) const
     {
-        const OUString* pFullName = pRefMgr->getExternalFileName(nFileId);
-        if (!pFullName)
-            return;
-
-        vector<OUString> aTabNames;
-        pRefMgr->getAllCachedTableNames(nFileId, aTabNames);
-        if (aTabNames.empty())
-            return;
-
         ScRange aAbsRef = rRef.toAbs(rPos);
 
-        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
-        ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, aTabNames, aAbsRef);
+        ConventionXL::makeExternalDocStr(rBuffer, rFileName, false);
+        ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, rTabNames, aAbsRef);
         rBuffer.append('!');
 
         if (!ValidCol(aAbsRef.aEnd.Col()) || !ValidRow(aAbsRef.aEnd.Row()))
@@ -4083,23 +4050,32 @@ void ScCompiler::CreateStringFromExternal(OUStringBuffer& rBuffer, FormulaToken*
 {
     FormulaToken* t = pTokenP;
     ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
+    const OUString* pFileName = pRefMgr->getExternalFileName(t->GetIndex());
+    if (!pFileName)
+        return;
+
     switch (t->GetType())
     {
         case svExternalName:
-        {
-            const OUString *pStr = pRefMgr->getExternalFileName(t->GetIndex());
-            OUString aFileName = pStr ? *pStr : ScGlobal::GetRscString(STR_NO_NAME_REF);
-            rBuffer.append(pConv->makeExternalNameStr( aFileName, t->GetString().getString()));
-        }
+            rBuffer.append(pConv->makeExternalNameStr(*pFileName, t->GetString().getString()));
         break;
         case svExternalSingleRef:
             pConv->makeExternalRefStr(
-                   rBuffer, GetPos(), t->GetIndex(), t->GetString().getString(), static_cast<ScToken*>(t)->GetSingleRef(), pRefMgr);
+                   rBuffer, GetPos(), *pFileName, t->GetString().getString(),
+                   static_cast<ScToken*>(t)->GetSingleRef());
         break;
         case svExternalDoubleRef:
+        {
+            vector<OUString> aTabNames;
+            pRefMgr->getAllCachedTableNames(t->GetIndex(), aTabNames);
+            if (aTabNames.empty())
+                return;
+
             pConv->makeExternalRefStr(
-                        rBuffer, GetPos(), t->GetIndex(), t->GetString().getString(), static_cast<ScToken*>(t)->GetDoubleRef(), pRefMgr);
-           break;
+                rBuffer, GetPos(), *pFileName, aTabNames, t->GetString().getString(),
+                static_cast<ScToken*>(t)->GetDoubleRef());
+        }
+        break;
         default:
             // warning, not error, otherwise we may end up with a never
             // ending message box loop if this was the cursor cell to be redrawn.
commit 905434f7af128790fe42d8d6ca44bcc1efb74c7b
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Mon Nov 18 13:27:01 2013 -0500

    Don't forget to generate RPN tokens here.
    
    Change-Id: I03a0e0d9f82e9c8881b0ff225ba0b476a693afb1

diff --git a/sc/source/core/tool/formulagroup.cxx b/sc/source/core/tool/formulagroup.cxx
index 072f162..8eb1fde 100644
--- a/sc/source/core/tool/formulagroup.cxx
+++ b/sc/source/core/tool/formulagroup.cxx
@@ -424,6 +424,8 @@ bool FormulaGroupInterpreterSoftware::interpret(ScDocument& rDoc, const ScAddres
         if (!pDest)
             return false;
 
+        ScCompiler aComp(&rDoc, aTmpPos, aCode2);
+        aComp.CompileTokenArray();
         ScInterpreter aInterpreter(pDest, &rDoc, aTmpPos, aCode2);
         aInterpreter.Interpret();
         aResults.push_back(aInterpreter.GetResultToken());
commit ccc32f4c4f226318912d597ff3e8bbafbaec7576
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 21:52:43 2013 -0500

    Try not to pass ScCompiler as a parameter.
    
    Change-Id: I7ef58f0455f11a3eb2ac88ec76e9436fb48a74e2

diff --git a/sc/inc/compiler.hxx b/sc/inc/compiler.hxx
index c780cca..b6251f7 100644
--- a/sc/inc/compiler.hxx
+++ b/sc/inc/compiler.hxx
@@ -252,11 +252,11 @@ public:
 
         virtual OUString makeExternalNameStr( const OUString& rFile, const OUString& rName ) const = 0;
 
-        virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+        virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                          sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
                                          ScExternalRefManager* pRefMgr ) const = 0;
 
-        virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+        virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                          sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
                                          ScExternalRefManager* pRefMgr ) const = 0;
 
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index dee954b..d3e696e 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -857,7 +857,7 @@ struct ConventionOOO_A1 : public Convention_A1
         return true;
     }
 
-    void makeExternalRefStrImpl( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    void makeExternalRefStrImpl( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
                                      ScExternalRefManager* pRefMgr, bool bODF ) const
     {
@@ -865,23 +865,23 @@ struct ConventionOOO_A1 : public Convention_A1
             rBuffer.append( '[');
 
         bool bEncodeUrl = bODF;
-        makeExternalSingleRefStr(rBuffer, nFileId, rTabName, rRef, rCompiler.GetPos(), pRefMgr, true, bEncodeUrl);
+        makeExternalSingleRefStr(rBuffer, nFileId, rTabName, rRef, rPos, pRefMgr, true, bEncodeUrl);
         if (bODF)
             rBuffer.append( ']');
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
-        makeExternalRefStrImpl( rBuffer, rCompiler, nFileId, rTabName, rRef, pRefMgr, false);
+        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, false);
     }
 
-    void makeExternalRefStrImpl( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    void makeExternalRefStrImpl( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
                                      ScExternalRefManager* pRefMgr, bool bODF ) const
     {
-        ScRange aAbsRange = rRef.toAbs(rCompiler.GetPos());
+        ScRange aAbsRange = rRef.toAbs(rPos);
 
         if (bODF)
             rBuffer.append( '[');
@@ -890,7 +890,7 @@ struct ConventionOOO_A1 : public Convention_A1
 
         do
         {
-            if (!makeExternalSingleRefStr(rBuffer, nFileId, rTabName, rRef.Ref1, rCompiler.GetPos(), pRefMgr, true, bEncodeUrl))
+            if (!makeExternalSingleRefStr(rBuffer, nFileId, rTabName, rRef.Ref1, rPos, pRefMgr, true, bEncodeUrl))
                 break;
 
             rBuffer.append(':');
@@ -916,17 +916,17 @@ struct ConventionOOO_A1 : public Convention_A1
             else if (bODF)
                 rBuffer.append( '.');      // need at least the sheet separator in ODF
             makeExternalSingleRefStr( rBuffer, nFileId, aLastTabName,
-                    rRef.Ref2, rCompiler.GetPos(), pRefMgr, bDisplayTabName, bEncodeUrl);
+                    rRef.Ref2, rPos, pRefMgr, bDisplayTabName, bEncodeUrl);
         } while (0);
         if (bODF)
             rBuffer.append( ']');
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
-        makeExternalRefStrImpl( rBuffer, rCompiler, nFileId, rTabName, rRef, pRefMgr, false);
+        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, false);
     }
 };
 
@@ -975,18 +975,18 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
         return lcl_makeExternalNameStr( rFile, rName, '#', true);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
-        makeExternalRefStrImpl( rBuffer, rCompiler, nFileId, rTabName, rRef, pRefMgr, true);
+        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, true);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
-        makeExternalRefStrImpl( rBuffer, rCompiler, nFileId, rTabName, rRef, pRefMgr, true);
+        makeExternalRefStrImpl(rBuffer, rPos, nFileId, rTabName, rRef, pRefMgr, true);
     }
 };
 
@@ -1277,7 +1277,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         return ConventionXL::makeExternalNameStr(rFile, rName);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
@@ -1295,10 +1295,10 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
         rBuffer.append('!');
 
-        makeSingleCellStr(rBuffer, rRef, rRef.toAbs(rCompiler.GetPos()));
+        makeSingleCellStr(rBuffer, rRef, rRef.toAbs(rPos));
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
@@ -1311,7 +1311,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         if (aTabNames.empty())
             return;
 
-        ScRange aAbsRef = rRef.toAbs(rCompiler.GetPos());
+        ScRange aAbsRef = rRef.toAbs(rPos);
 
         ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
         ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, aTabNames, aAbsRef);
@@ -1470,7 +1470,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         return ConventionXL::makeExternalNameStr(rFile, rName);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
@@ -1484,7 +1484,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         if (!pFullName)
             return;
 
-        ScAddress aAbsRef = rRef.toAbs(rCompiler.GetPos());
+        ScAddress aAbsRef = rRef.toAbs(rPos);
 
         ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
@@ -1494,7 +1494,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         r1c1_add_col(rBuffer, rRef, aAbsRef);
     }
 
-    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
+    virtual void makeExternalRefStr( OUStringBuffer& rBuffer, const ScAddress& rPos,
                                      sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef,
                                      ScExternalRefManager* pRefMgr ) const
     {
@@ -1507,7 +1507,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         if (aTabNames.empty())
             return;
 
-        ScRange aAbsRef = rRef.toAbs(rCompiler.GetPos());
+        ScRange aAbsRef = rRef.toAbs(rPos);
 
         ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
         ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, aTabNames, aAbsRef);
@@ -4094,11 +4094,11 @@ void ScCompiler::CreateStringFromExternal(OUStringBuffer& rBuffer, FormulaToken*
         break;
         case svExternalSingleRef:
             pConv->makeExternalRefStr(
-                   rBuffer, *this, t->GetIndex(), t->GetString().getString(), static_cast<ScToken*>(t)->GetSingleRef(), pRefMgr);
+                   rBuffer, GetPos(), t->GetIndex(), t->GetString().getString(), static_cast<ScToken*>(t)->GetSingleRef(), pRefMgr);
         break;
         case svExternalDoubleRef:
             pConv->makeExternalRefStr(
-                        rBuffer, *this, t->GetIndex(), t->GetString().getString(), static_cast<ScToken*>(t)->GetDoubleRef(), pRefMgr);
+                        rBuffer, GetPos(), t->GetIndex(), t->GetString().getString(), static_cast<ScToken*>(t)->GetDoubleRef(), pRefMgr);
            break;
         default:
             // warning, not error, otherwise we may end up with a never
commit 066d33c7bdcaa938ba7832e53944e1e1a73576cc
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 21:07:09 2013 -0500

    EncodeUrlMode always set to "by grammer". Remove it.
    
    Change-Id: I7de308bbdaacc1fc60a142497acc2301c080e5dc

diff --git a/sc/inc/compiler.hxx b/sc/inc/compiler.hxx
index 0fed5cb..c780cca 100644
--- a/sc/inc/compiler.hxx
+++ b/sc/inc/compiler.hxx
@@ -213,13 +213,6 @@ class SC_DLLPUBLIC ScCompiler : public formula::FormulaCompiler
 {
 public:
 
-    enum EncodeUrlMode
-    {
-        ENCODE_BY_GRAMMAR,
-        ENCODE_ALWAYS,
-        ENCODE_NEVER,
-    };
-
     enum ExtendedErrorDetection
     {
         EXTENDED_ERROR_DETECTION_NONE = 0,      // no error on unknown symbols, default (interpreter handles it)
@@ -330,7 +323,6 @@ private:
     SCsTAB      nMaxTab;                    // last sheet in document
     sal_Int32   mnRangeOpPosInSymbol;       // if and where a range operator is in symbol
     const Convention *pConv;
-    EncodeUrlMode   meEncodeUrlMode;
     ExtendedErrorDetection  meExtendedErrorDetection;
     bool        mbCloseBrackets;            // whether to close open brackets automatically, default TRUE
     bool        mbRewind;                   // whether symbol is to be rewound to some step during lexical analysis
@@ -411,7 +403,7 @@ public:
     void            SetGrammar( const formula::FormulaGrammar::Grammar eGrammar );
 
     void SetNumberFormatter( SvNumberFormatter* pFormatter );
-    EncodeUrlMode   GetEncodeUrlMode() const;
+
 private:
     /** Set grammar and reference convention from within SetFormulaLanguage()
         or SetGrammar().
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index 39b4987..dee954b 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -244,11 +244,6 @@ void ScCompiler::SetNumberFormatter( SvNumberFormatter* pFormatter )
     mpFormatter = pFormatter;
 }
 
-ScCompiler::EncodeUrlMode ScCompiler::GetEncodeUrlMode() const
-{
-    return meEncodeUrlMode;
-}
-
 void ScCompiler::SetFormulaLanguage( const ScCompiler::OpCodeMapPtr & xMap )
 {
     if (xMap.get())
@@ -869,21 +864,7 @@ struct ConventionOOO_A1 : public Convention_A1
         if (bODF)
             rBuffer.append( '[');
 
-        bool bEncodeUrl = true;
-        switch (rCompiler.GetEncodeUrlMode())
-        {
-            case ScCompiler::ENCODE_BY_GRAMMAR:
-                bEncodeUrl = bODF;
-            break;
-            case ScCompiler::ENCODE_ALWAYS:
-                bEncodeUrl = true;
-            break;
-            case ScCompiler::ENCODE_NEVER:
-                bEncodeUrl = false;
-            break;
-            default:
-                ;
-        }
+        bool bEncodeUrl = bODF;
         makeExternalSingleRefStr(rBuffer, nFileId, rTabName, rRef, rCompiler.GetPos(), pRefMgr, true, bEncodeUrl);
         if (bODF)
             rBuffer.append( ']');
@@ -905,21 +886,7 @@ struct ConventionOOO_A1 : public Convention_A1
         if (bODF)
             rBuffer.append( '[');
         // Ensure that there's always a closing bracket, no premature returns.
-        bool bEncodeUrl = true;
-        switch (rCompiler.GetEncodeUrlMode())
-        {
-            case ScCompiler::ENCODE_BY_GRAMMAR:
-                bEncodeUrl = bODF;
-            break;
-            case ScCompiler::ENCODE_ALWAYS:
-                bEncodeUrl = true;
-            break;
-            case ScCompiler::ENCODE_NEVER:
-                bEncodeUrl = false;
-            break;
-            default:
-                ;
-        }
+        bool bEncodeUrl = bODF;
 
         do
         {
@@ -1324,8 +1291,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         if (!pFullName)
             return;
 
-        ConventionXL::makeExternalDocStr(
-            rBuffer, *pFullName, rCompiler.GetEncodeUrlMode() == ScCompiler::ENCODE_ALWAYS);
+        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
         rBuffer.append('!');
 
@@ -1347,8 +1313,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
 
         ScRange aAbsRef = rRef.toAbs(rCompiler.GetPos());
 
-        ConventionXL::makeExternalDocStr(
-            rBuffer, *pFullName, rCompiler.GetEncodeUrlMode() == ScCompiler::ENCODE_ALWAYS);
+        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
         ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, aTabNames, aAbsRef);
         rBuffer.append('!');
 
@@ -1521,8 +1486,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
 
         ScAddress aAbsRef = rRef.toAbs(rCompiler.GetPos());
 
-        ConventionXL::makeExternalDocStr(
-            rBuffer, *pFullName, rCompiler.GetEncodeUrlMode() == ScCompiler::ENCODE_ALWAYS);
+        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
         ScRangeStringConverter::AppendTableName(rBuffer, rTabName);
         rBuffer.append('!');
 
@@ -1545,8 +1509,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
 
         ScRange aAbsRef = rRef.toAbs(rCompiler.GetPos());
 
-        ConventionXL::makeExternalDocStr(
-            rBuffer, *pFullName, rCompiler.GetEncodeUrlMode() == ScCompiler::ENCODE_ALWAYS);
+        ConventionXL::makeExternalDocStr(rBuffer, *pFullName, false);
         ConventionXL::makeExternalTabNameRange(rBuffer, rTabName, aTabNames, aAbsRef);
         rBuffer.append('!');
 
@@ -1607,7 +1570,6 @@ ScCompiler::ScCompiler( ScDocument* pDocument, const ScAddress& rPos,ScTokenArra
         mnPredetectedReference(0),
         mnRangeOpPosInSymbol(-1),
         pConv( pConvOOO_A1 ),
-        meEncodeUrlMode( ENCODE_BY_GRAMMAR ),
         meExtendedErrorDetection( EXTENDED_ERROR_DETECTION_NONE ),
         mbCloseBrackets( true ),
         mbRewind( false )
@@ -1634,7 +1596,6 @@ ScCompiler::ScCompiler( ScDocument* pDocument, const ScAddress& rPos)
         mnPredetectedReference(0),
         mnRangeOpPosInSymbol(-1),
         pConv( pConvOOO_A1 ),
-        meEncodeUrlMode( ENCODE_BY_GRAMMAR ),
         meExtendedErrorDetection( EXTENDED_ERROR_DETECTION_NONE ),
         mbCloseBrackets( true ),
         mbRewind( false )
commit 3dab6fcbedf21c1d2971527f6f99fa46d3d45514
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 19:41:47 2013 -0500

    Handle matrix.
    
    Change-Id: I084b2348b5c52151b0d0ea36d082ec2f2603e0d4

diff --git a/sc/qa/unit/ucalc_formula.cxx b/sc/qa/unit/ucalc_formula.cxx
index 91e4594..7ae2554 100644
--- a/sc/qa/unit/ucalc_formula.cxx
+++ b/sc/qa/unit/ucalc_formula.cxx
@@ -113,7 +113,8 @@ void Test::testFormulaCreateStringFromTokens()
         "'Past Data'.B1+'2013'.B2*(1+'Kevin''s Data'.C10)",
         "x+y*z", // named ranges
         "SUM(sheetx;x;y;z)", // sheet local and global named ranges mixed
-        "MAX(Table1)+MIN(Table2)*SUM(Table3)" // database ranges
+        "MAX(Table1)+MIN(Table2)*SUM(Table3)", // database ranges
+        "{1;TRUE;3|FALSE;5;\"Text\"|;;}", // inline matrix
     };
 
     boost::scoped_ptr<ScTokenArray> pArray;
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index b1d7e94..fcd2aea 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -3193,6 +3193,30 @@ void ScTokenArray::CheckRelativeReferenceBounds(
 
 namespace {
 
+void appendDouble( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, double fVal )
+{
+    if (rCxt.mxOpCodeMap->isEnglish())
+    {
+        rtl::math::doubleToUStringBuffer(
+            rBuf, fVal, rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max, '.', true);
+    }
+    else
+    {
+        SvtSysLocale aSysLocale;
+        rtl::math::doubleToUStringBuffer(
+            rBuf, fVal,
+            rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
+            aSysLocale.GetLocaleDataPtr()->getNumDecimalSep()[0], true);
+    }
+}
+
+void appendString( OUStringBuffer& rBuf, const OUString& rStr )
+{
+    rBuf.append(sal_Unicode('"'));
+    rBuf.append(rStr.replaceAll("\"", "\"\""));
+    rBuf.append(sal_Unicode('"'));
+}
+
 void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, const FormulaToken& rToken, const ScAddress& rPos )
 {
     if (rToken.IsExternalRef())
@@ -3205,21 +3229,7 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
     switch (rToken.GetType())
     {
         case svDouble:
-        {
-            if (rCxt.mxOpCodeMap->isEnglish())
-            {
-                rtl::math::doubleToUStringBuffer(
-                    rBuf, rToken.GetDouble(), rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max, '.', true);
-            }
-            else
-            {
-                SvtSysLocale aSysLocale;
-                rtl::math::doubleToUStringBuffer(
-                    rBuf, rToken.GetDouble(),
-                    rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
-                    aSysLocale.GetLocaleDataPtr()->getNumDecimalSep()[0], true);
-            }
-        }
+            appendDouble(rCxt, rBuf, rToken.GetDouble());
         break;
         case svString:
         {
@@ -3230,9 +3240,7 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
                 return;
             }
 
-            rBuf.append(sal_Unicode('"'));
-            rBuf.append(aStr.replaceAll("\"", "\"\""));
-            rBuf.append(sal_Unicode('"'));
+            appendString(rBuf, aStr);
         }
         break;
         case svSingleRef:
@@ -3261,7 +3269,55 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
         }
         break;
         case svMatrix:
-            // TODO : Implement this.
+        {
+            const ScMatrix* pMat = static_cast<const ScToken&>(rToken).GetMatrix();
+            if (!pMat)
+                return;
+
+            size_t nC, nMaxC, nR, nMaxR;
+            pMat->GetDimensions(nMaxC, nMaxR);
+
+            rBuf.append(rCxt.mxOpCodeMap->getSymbol(ocArrayOpen));
+            for (nR = 0 ; nR < nMaxR ; ++nR)
+            {
+                if (nR > 0)
+                {
+                    rBuf.append(rCxt.mxOpCodeMap->getSymbol(ocArrayRowSep));
+                }
+
+                for (nC = 0 ; nC < nMaxC ; ++nC)
+                {
+                    if (nC > 0)
+                    {
+                        rBuf.append(rCxt.mxOpCodeMap->getSymbol(ocArrayColSep));
+                    }
+
+                    if (pMat->IsValue(nC, nR))
+                    {
+                        if (pMat->IsBoolean(nC, nR))
+                        {
+                            bool bVal = pMat->GetDouble(nC, nR) != 0.0;
+                            rBuf.append(rCxt.mxOpCodeMap->getSymbol(bVal ? ocTrue : ocFalse));
+                        }
+                        else
+                        {
+                            sal_uInt16 nErr = pMat->GetError(nC, nR);
+                            if (nErr)
+                                rBuf.append(ScGlobal::GetErrorString(nErr));
+                            else
+                                appendDouble(rCxt, rBuf, pMat->GetDouble(nC, nR));
+                        }
+                    }
+                    else if (pMat->IsEmpty(nC, nR))
+                    {
+                        // Skip it.
+                    }
+                    else if (pMat->IsString(nC, nR))
+                        appendString(rBuf, pMat->GetString(nC, nR).getString());
+                }
+            }
+            rBuf.append(rCxt.mxOpCodeMap->getSymbol(ocArrayClose));
+        }
         break;
         case svIndex:
         {
commit 3f5987421e4d612012ec346ae280fb362239a5d6
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 19:05:48 2013 -0500

    Fix indentation.
    
    Change-Id: I260649aca445e03c52423d8f0a63eb4681d2a876

diff --git a/formula/source/core/api/FormulaCompiler.cxx b/formula/source/core/api/FormulaCompiler.cxx
index 480d557..e8c40aa 100644
--- a/formula/source/core/api/FormulaCompiler.cxx
+++ b/formula/source/core/api/FormulaCompiler.cxx
@@ -1843,9 +1843,9 @@ FormulaToken* FormulaCompiler::CreateStringFromToken( OUStringBuffer& rBuffer, F
                     }
                 }
                 if (!bMapped && !mxSymbols->isEnglish())
-                        LocalizeString( aAddIn );
-                    rBuffer.append( aAddIn);
-                }
+                    LocalizeString( aAddIn );
+                rBuffer.append( aAddIn);
+            }
             break;
             case svError:
                 AppendErrorConstant( rBuffer, t->GetError());
commit af7f6edd2fa36c8e28c43e707e30361fa3f7bf25
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 19:03:56 2013 -0500

    Handle external functions (add-ins).
    
    Unfortunately we can't test this piece easily just yet.  Enabling add-in
    functions would mess up the function list which is unit-tested and thus
    fail if we enable add-ins in unit test.
    
    Change-Id: Ieda5e5560d4c7e68d7c6272c5d85f2ac63bd4ee4

diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index 294dd65..9eef1ee 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -2552,7 +2552,7 @@ void Test::testFunctionLists()
         for (sal_uInt32 j = 0; j < nFuncCount; ++j)
         {
             const formula::IFunctionDescription* pFunc = pCat->getFunction(j);
-            CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected function name", pFunc->getFunctionName(), OUString::createFromAscii(aTests[i].Functions[j]));
+            CPPUNIT_ASSERT_EQUAL_MESSAGE("Unexpected function name", OUString::createFromAscii(aTests[i].Functions[j]), pFunc->getFunctionName());
         }
     }
 }
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index 89efbc3..b1d7e94 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -41,6 +41,7 @@
 #include "tokenstringcontext.hxx"
 #include "types.hxx"
 #include "globstr.hrc"
+#include "addincol.hxx"
 #include "svl/sharedstring.hxx"
 
 using ::std::vector;
@@ -3318,7 +3319,26 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
         }
         break;
         case svExternal:
-            // TODO : Implement this.
+        {
+            // mapped or translated name of AddIns
+            OUString aAddIn = rToken.GetExternal();
+            bool bMapped = rCxt.mxOpCodeMap->isPODF();     // ODF 1.1 directly uses programmatical name
+            if (!bMapped && rCxt.mxOpCodeMap->hasExternals())
+            {
+                const ExternalHashMap& rExtMap = *rCxt.mxOpCodeMap->getReverseExternalHashMap();
+                ExternalHashMap::const_iterator it = rExtMap.find(aAddIn);
+                if (it != rExtMap.end())
+                {
+                    aAddIn = it->second;
+                    bMapped = true;
+                }
+            }
+
+            if (!bMapped && !rCxt.mxOpCodeMap->isEnglish())
+                ScGlobal::GetAddInCollection()->LocalizeString(aAddIn);
+
+            rBuf.append(aAddIn);
+        }
         break;
         case svError:
         {
commit 0df2c02d4f40b627f17d68d80a18999f4f40aea2
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 18:28:32 2013 -0500

    Let's have makeRefStr() take individual parameters again.
    
    And remove use of TokenStringContext from ScCompiler.
    
    Change-Id: Ib0636e2437a64edd372623a7176dab462eed831b

diff --git a/sc/inc/compiler.hxx b/sc/inc/compiler.hxx
index ee3c689..0fed5cb 100644
--- a/sc/inc/compiler.hxx
+++ b/sc/inc/compiler.hxx
@@ -78,12 +78,6 @@ class ScRangeData;
 class ScExternalRefManager;
 class ScTokenArray;
 
-namespace sc {
-
-struct TokenStringContext;
-
-}
-
 // constants and data types internal to compiler
 
 /*
@@ -241,7 +235,10 @@ public:
         virtual ~Convention();
 
         virtual void makeRefStr(
-            OUStringBuffer& rBuffer, const ScAddress& rPos, const sc::TokenStringContext& rCxt,
+            OUStringBuffer& rBuffer,
+            formula::FormulaGrammar::Grammar eGram,
+            const ScAddress& rPos,
+            const OUString& rErrRef, const std::vector<OUString>& rTabNames,
             const ScComplexRefData& rRef, bool bSingleRef ) const = 0;
 
         virtual ::com::sun::star::i18n::ParseResult
@@ -338,7 +335,7 @@ private:
     bool        mbCloseBrackets;            // whether to close open brackets automatically, default TRUE
     bool        mbRewind;                   // whether symbol is to be rewound to some step during lexical analysis
     std::vector<sal_uInt16> maExternalFiles;
-    mutable sc::TokenStringContext* mpTokenStringCxt;
+    std::vector<OUString> maTabNames;
 
     bool   NextNewToken(bool bInArray = false);
 
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index a9d17d6..39b4987 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -67,7 +67,6 @@
 #include "formulaparserpool.hxx"
 #include "tokenarray.hxx"
 #include "scmatrix.hxx"
-#include "tokenstringcontext.hxx"
 
 using namespace formula;
 using namespace ::com::sun::star;
@@ -728,19 +727,20 @@ struct ConventionOOO_A1 : public Convention_A1
     ConventionOOO_A1() : Convention_A1 (FormulaGrammar::CONV_OOO) { }
     ConventionOOO_A1( FormulaGrammar::AddressConvention eConv ) : Convention_A1 (eConv) { }
 
-    static OUString MakeTabStr( const sc::TokenStringContext& rCxt, SCTAB nTab )
+    static OUString MakeTabStr( const std::vector<OUString>& rTabNames, SCTAB nTab )
     {
         OUString aString;
-        if (static_cast<size_t>(nTab) >= rCxt.maTabNames.size())
+        if (static_cast<size_t>(nTab) >= rTabNames.size())
             aString = ScGlobal::GetRscString(STR_NO_REF_TABLE);
         else
-            aString = rCxt.maTabNames[nTab];
+            aString = rTabNames[nTab];
         aString += ".";
         return aString;
     }
 
     void MakeOneRefStrImpl(
-        OUStringBuffer& rBuffer, const sc::TokenStringContext& rCxt,
+        OUStringBuffer& rBuffer,
+        const OUString& rErrRef, const std::vector<OUString>& rTabNames,
         const ScSingleRefData& rRef, const ScAddress& rAbsRef,
         bool bForceTab, bool bODF ) const
     {
@@ -750,12 +750,12 @@ struct ConventionOOO_A1 : public Convention_A1
             {
                 if (!rRef.IsTabRel())
                     rBuffer.append('$');
-                rBuffer.append(rCxt.maErrRef);
+                rBuffer.append(rErrRef);
                 rBuffer.append('.');
             }
             else
             {
-                OUString aRefStr(MakeTabStr(rCxt, rAbsRef.Tab()));
+                OUString aRefStr(MakeTabStr(rTabNames, rAbsRef.Tab()));
                 if (!rRef.IsTabRel())
                     rBuffer.append('$');
                 rBuffer.append(aRefStr);
@@ -766,20 +766,21 @@ struct ConventionOOO_A1 : public Convention_A1
         if (!rRef.IsColRel())
             rBuffer.append('$');
         if (!ValidCol(rAbsRef.Col()))
-            rBuffer.append(rCxt.maErrRef);
+            rBuffer.append(rErrRef);
         else
             MakeColStr(rBuffer, rAbsRef.Col());
         if (!rRef.IsRowRel())
             rBuffer.append('$');
         if (!ValidRow(rAbsRef.Row()))
-            rBuffer.append(rCxt.maErrRef);
+            rBuffer.append(rErrRef);
         else
             MakeRowStr(rBuffer, rAbsRef.Row());
     }
 
     void makeRefStr( OUStringBuffer&   rBuffer,
+                     formula::FormulaGrammar::Grammar /*eGram*/,
                      const ScAddress& rPos,
-                     const sc::TokenStringContext& rCxt,
+                     const OUString& rErrRef, const std::vector<OUString>& rTabNames,
                      const ScComplexRefData& rRef,
                      bool bSingleRef ) const
     {
@@ -790,11 +791,11 @@ struct ConventionOOO_A1 : public Convention_A1
         if( !bSingleRef )
             aAbs2 = aRef.Ref2.toAbs(rPos);
 
-        MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref1, aAbs1, false, false);
+        MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref1, aAbs1, false, false);
         if (!bSingleRef)
         {
             rBuffer.append(':');
-            MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), false);
+            MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), false);
         }
     }
 
@@ -969,8 +970,9 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
 {
     ConventionOOO_A1_ODF() : ConventionOOO_A1 (FormulaGrammar::CONV_ODF) { }
     void makeRefStr( OUStringBuffer&   rBuffer,
+                     formula::FormulaGrammar::Grammar eGram,
                      const ScAddress& rPos,
-                     const sc::TokenStringContext& rCxt,
+                     const OUString& rErrRef, const std::vector<OUString>& rTabNames,
                      const ScComplexRefData& rRef,
                      bool bSingleRef ) const
     {
@@ -982,20 +984,20 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
         if( !bSingleRef )
             aAbs2 = aRef.Ref2.toAbs(rPos);
 
-        if (FormulaGrammar::isODFF(rCxt.meGram) && (!ValidAddress(aAbs1) || !ValidAddress(aAbs2)))
+        if (FormulaGrammar::isODFF(eGram) && (!ValidAddress(aAbs1) || !ValidAddress(aAbs2)))
         {
-            rBuffer.append(rCxt.maErrRef);
+            rBuffer.append(rErrRef);
             // For ODFF write [#REF!], but not for PODF so apps reading ODF
             // 1.0/1.1 may have a better chance if they implemented the old
             // form.
         }
         else
         {
-            MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref1, aAbs1, false, true);
+            MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref1, aAbs1, false, true);
             if (!bSingleRef)
             {
                 rBuffer.append(sal_Unicode(':'));
-                MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), true);
+                MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), true);
             }
         }
         rBuffer.append(sal_Unicode(']'));
@@ -1027,21 +1029,21 @@ const ScCompiler::Convention * const ScCompiler::pConvOOO_A1_ODF = &ConvOOO_A1_O
 struct ConventionXL
 {
     static void GetTab(
-        const ScAddress& rPos, const sc::TokenStringContext& rCxt,
+        const ScAddress& rPos, const std::vector<OUString>& rTabNames,
         const ScSingleRefData& rRef, OUString& rTabName )
     {
         ScAddress aAbs = rRef.toAbs(rPos);
-        if (rRef.IsTabDeleted() || static_cast<size_t>(aAbs.Tab()) >= rCxt.maTabNames.size())
+        if (rRef.IsTabDeleted() || static_cast<size_t>(aAbs.Tab()) >= rTabNames.size())
         {
             rTabName = ScGlobal::GetRscString( STR_NO_REF_TABLE );
             return;
         }
-        rTabName = rCxt.maTabNames[aAbs.Tab()];
+        rTabName = rTabNames[aAbs.Tab()];
     }
 
     static void MakeTabStr( OUStringBuffer& rBuf,
                             const ScAddress& rPos,
-                            const sc::TokenStringContext& rCxt,
+                            const std::vector<OUString>& rTabNames,
                             const ScComplexRefData& rRef,
                             bool bSingleRef )
     {
@@ -1049,11 +1051,11 @@ struct ConventionXL
         {
             OUString aStartTabName, aEndTabName;
 
-            GetTab(rPos, rCxt, rRef.Ref1, aStartTabName);
+            GetTab(rPos, rTabNames, rRef.Ref1, aStartTabName);
 
             if( !bSingleRef && rRef.Ref2.IsFlag3D() )
             {
-                GetTab(rPos, rCxt, rRef.Ref2, aEndTabName);
+                GetTab(rPos, rTabNames, rRef.Ref2, aEndTabName);
             }
 
             rBuf.append( aStartTabName );
@@ -1211,8 +1213,9 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
     }
 
     void makeRefStr( OUStringBuffer&   rBuf,
+                     formula::FormulaGrammar::Grammar /*eGram*/,
                      const ScAddress& rPos,
-                     const sc::TokenStringContext& rCxt,
+                     const OUString& /*rErrRef*/, const std::vector<OUString>& rTabNames,
                      const ScComplexRefData& rRef,
                      bool bSingleRef ) const
     {
@@ -1222,7 +1225,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         // Foo!A1:#REF! versus #REF! at this point
         ScAddress aAbs1 = aRef.Ref1.toAbs(rPos), aAbs2;
 
-        MakeTabStr(rBuf, rPos, rCxt, aRef, bSingleRef);
+        MakeTabStr(rBuf, rPos, rTabNames, aRef, bSingleRef);
 
         if (!ValidAddress(aAbs1))
         {
@@ -1401,15 +1404,16 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
 {
     ConventionXL_R1C1() : ScCompiler::Convention( FormulaGrammar::CONV_XL_R1C1 ) { }
     void makeRefStr( OUStringBuffer&   rBuf,
+                     formula::FormulaGrammar::Grammar /*eGram*/,
                      const ScAddress& rPos,
-                     const sc::TokenStringContext& rCxt,
+                     const OUString& /*rErrRef*/, const std::vector<OUString>& rTabNames,
                      const ScComplexRefData& rRef,
                      bool bSingleRef ) const
     {
         ScRange aAbsRef = rRef.toAbs(rPos);
         ScComplexRefData aRef( rRef );
 
-        MakeTabStr(rBuf, rPos, rCxt, aRef, bSingleRef);
+        MakeTabStr(rBuf, rPos, rTabNames, aRef, bSingleRef);
 
         // Play fast and loose with invalid refs.  There is not much point in producing
         // Foo!A1:#REF! versus #REF! at this point
@@ -1606,10 +1610,19 @@ ScCompiler::ScCompiler( ScDocument* pDocument, const ScAddress& rPos,ScTokenArra
         meEncodeUrlMode( ENCODE_BY_GRAMMAR ),
         meExtendedErrorDetection( EXTENDED_ERROR_DETECTION_NONE ),
         mbCloseBrackets( true ),
-        mbRewind( false ),
-    mpTokenStringCxt(NULL)
+        mbRewind( false )
 {
     nMaxTab = pDoc ? pDoc->GetTableCount() - 1 : 0;
+
+    if (pDoc)
+    {
+        maTabNames = pDoc->GetAllTableNames();
+        {
+            std::vector<OUString>::iterator it = maTabNames.begin(), itEnd = maTabNames.end();
+            for (; it != itEnd; ++it)
+                ScCompiler::CheckTabQuotes(*it, formula::FormulaGrammar::extractRefConvention(meGrammar));
+        }
+    }
 }
 
 ScCompiler::ScCompiler( ScDocument* pDocument, const ScAddress& rPos)
@@ -1624,15 +1637,23 @@ ScCompiler::ScCompiler( ScDocument* pDocument, const ScAddress& rPos)
         meEncodeUrlMode( ENCODE_BY_GRAMMAR ),
         meExtendedErrorDetection( EXTENDED_ERROR_DETECTION_NONE ),
         mbCloseBrackets( true ),
-        mbRewind( false ),
-    mpTokenStringCxt(NULL)
+        mbRewind( false )
 {
     nMaxTab = pDoc ? pDoc->GetTableCount() - 1 : 0;
+
+    if (pDoc)
+    {
+        maTabNames = pDoc->GetAllTableNames();
+        {
+            std::vector<OUString>::iterator it = maTabNames.begin(), itEnd = maTabNames.end();
+            for (; it != itEnd; ++it)
+                ScCompiler::CheckTabQuotes(*it, formula::FormulaGrammar::extractRefConvention(meGrammar));
+        }
+    }
 }
 
 ScCompiler::~ScCompiler()
 {
-    delete mpTokenStringCxt;
 }
 
 void ScCompiler::CheckTabQuotes( OUString& rString,
@@ -4172,9 +4193,7 @@ void ScCompiler::CreateStringFromMatrix(
 
 void ScCompiler::CreateStringFromSingleRef(OUStringBuffer& rBuffer,FormulaToken* _pTokenP) const
 {
-    if (!mpTokenStringCxt)
-        mpTokenStringCxt = new sc::TokenStringContext(pDoc, meGrammar);
-
+    OUString aErrRef = GetCurrentOpCodeMap()->getSymbol(ocErrRef);
     const OpCode eOp = _pTokenP->GetOpCode();
     const ScSingleRefData& rRef = static_cast<const ScToken*>(_pTokenP)->GetSingleRef();
     ScComplexRefData aRef;
@@ -4191,19 +4210,17 @@ void ScCompiler::CreateStringFromSingleRef(OUStringBuffer& rBuffer,FormulaToken*
         else
         {
             rBuffer.append(ScGlobal::GetRscString(STR_NO_NAME_REF));
-            pConv->makeRefStr(rBuffer, aPos, *mpTokenStringCxt, aRef, true);
+            pConv->makeRefStr(rBuffer, meGrammar, aPos, aErrRef, maTabNames, aRef, true);
         }
     }
     else
-        pConv->makeRefStr(rBuffer, aPos, *mpTokenStringCxt, aRef, true);
+        pConv->makeRefStr(rBuffer, meGrammar, aPos, aErrRef, maTabNames, aRef, true);
 }
 
 void ScCompiler::CreateStringFromDoubleRef(OUStringBuffer& rBuffer,FormulaToken* _pTokenP) const
 {
-    if (!mpTokenStringCxt)
-        mpTokenStringCxt = new sc::TokenStringContext(pDoc, meGrammar);
-
-    pConv->makeRefStr(rBuffer, aPos, *mpTokenStringCxt, static_cast<ScToken*>(_pTokenP)->GetDoubleRef(), false);
+    OUString aErrRef = GetCurrentOpCodeMap()->getSymbol(ocErrRef);
+    pConv->makeRefStr(rBuffer, meGrammar, aPos, aErrRef, maTabNames, static_cast<ScToken*>(_pTokenP)->GetDoubleRef(), false);
 }
 
 void ScCompiler::CreateStringFromIndex(OUStringBuffer& rBuffer,FormulaToken* _pTokenP) const
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index 9506148..89efbc3 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -3242,7 +3242,7 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
                 ScComplexRefData aRef;
                 aRef.Ref1 = rRef;
                 aRef.Ref2 = rRef;
-                rCxt.mpRefConv->makeRefStr(rBuf, rPos, rCxt, aRef, true);
+                rCxt.mpRefConv->makeRefStr(rBuf, rCxt.meGram, rPos, rCxt.maErrRef, rCxt.maTabNames, aRef, true);
             }
             else
                 rBuf.append(rCxt.maErrRef);
@@ -3253,7 +3253,7 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
             if (rCxt.mpRefConv)
             {
                 const ScComplexRefData& rRef = static_cast<const ScToken&>(rToken).GetDoubleRef();
-                rCxt.mpRefConv->makeRefStr(rBuf, rPos, rCxt, rRef, false);
+                rCxt.mpRefConv->makeRefStr(rBuf, rCxt.meGram, rPos, rCxt.maErrRef, rCxt.maTabNames, rRef, false);
             }
             else
                 rBuf.append(rCxt.maErrRef);
commit 62b36e800d650ede5d48d8a2b81a410578407b32
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 16:16:04 2013 -0500

    Handle sheet-local range names too.
    
    Change-Id: Ib1503c3b69d77946b4437bdc0e1bfa5ebacbb602

diff --git a/sc/inc/tokenstringcontext.hxx b/sc/inc/tokenstringcontext.hxx
index 6015a59..7af90eb 100644
--- a/sc/inc/tokenstringcontext.hxx
+++ b/sc/inc/tokenstringcontext.hxx
@@ -27,6 +27,7 @@ namespace sc {
 struct SC_DLLPUBLIC TokenStringContext
 {
     typedef boost::unordered_map<sal_uInt16, OUString> IndexNameMapType;
+    typedef boost::unordered_map<SCTAB, IndexNameMapType> TabIndexMapType;
 
     formula::FormulaGrammar::Grammar meGram;
     formula::FormulaCompiler::OpCodeMapPtr mxOpCodeMap;
@@ -35,6 +36,7 @@ struct SC_DLLPUBLIC TokenStringContext
 
     std::vector<OUString> maTabNames;
     IndexNameMapType maGlobalRangeNames;
+    TabIndexMapType maSheetRangeNames;
     IndexNameMapType maNamedDBs;
 
     TokenStringContext( const ScDocument* pDoc, formula::FormulaGrammar::Grammar eGram );
diff --git a/sc/qa/unit/ucalc_formula.cxx b/sc/qa/unit/ucalc_formula.cxx
index 35a3599..91e4594 100644
--- a/sc/qa/unit/ucalc_formula.cxx
+++ b/sc/qa/unit/ucalc_formula.cxx
@@ -45,16 +45,21 @@ void Test::testFormulaCreateStringFromTokens()
 
     // Insert named ranges.
     struct {
+        bool bGlobal;
         const char* pName;
         const char* pExpr;
     } aNames[] = {
-        { "x", "Test.H1" },
-        { "y", "Test.H2" },
-        { "z", "Test.H3" }
+        { true, "x", "Test.H1" },
+        { true, "y", "Test.H2" },
+        { true, "z", "Test.H3" },
+
+        { false, "sheetx", "Test.J1" }
     };
 
     ScRangeName* pGlobalNames = m_pDoc->GetRangeName();
+    ScRangeName* pSheetNames = m_pDoc->GetRangeName(0);
     CPPUNIT_ASSERT_MESSAGE("Failed to obtain global named expression object.", pGlobalNames);
+    CPPUNIT_ASSERT_MESSAGE("Failed to obtain sheet-local named expression object.", pSheetNames);
 
     for (size_t i = 0, n = SAL_N_ELEMENTS(aNames); i < n; ++i)
     {
@@ -62,8 +67,16 @@ void Test::testFormulaCreateStringFromTokens()
             m_pDoc, OUString::createFromAscii(aNames[i].pName), OUString::createFromAscii(aNames[i].pExpr),
             ScAddress(0,0,0), RT_NAME, formula::FormulaGrammar::GRAM_NATIVE);
 
-        bool bInserted = pGlobalNames->insert(pName);
-        CPPUNIT_ASSERT_MESSAGE("Failed to insert a new name.", bInserted);
+        if (aNames[i].bGlobal)
+        {
+            bool bInserted = pGlobalNames->insert(pName);
+            CPPUNIT_ASSERT_MESSAGE("Failed to insert a new name.", bInserted);
+        }
+        else
+        {
+            bool bInserted = pSheetNames->insert(pName);
+            CPPUNIT_ASSERT_MESSAGE("Failed to insert a new name.", bInserted);
+        }
     }
 
     // Insert DB ranges.
@@ -99,6 +112,7 @@ void Test::testFormulaCreateStringFromTokens()
         "'Kevin''s Data'.B10",
         "'Past Data'.B1+'2013'.B2*(1+'Kevin''s Data'.C10)",
         "x+y*z", // named ranges
+        "SUM(sheetx;x;y;z)", // sheet local and global named ranges mixed
         "MAX(Table1)+MIN(Table2)*SUM(Table3)" // database ranges
     };
 
diff --git a/sc/source/core/data/documen3.cxx b/sc/source/core/data/documen3.cxx
index 73f8d71..e76b78c 100644
--- a/sc/source/core/data/documen3.cxx
+++ b/sc/source/core/data/documen3.cxx
@@ -109,7 +109,7 @@ void ScDocument::GetAllTabRangeNames(ScRangeName::TabNameCopyMap& rNames) const
             // no more tables to iterate through.
             break;
 
-        const ScRangeName* p = maTabs[i]->GetRangeName();
+        const ScRangeName* p = maTabs[i]->mpRangeName;
         if (!p || p->empty())
             // ignore empty ones.
             continue;
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index 4966369..9506148 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -3264,19 +3264,50 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
         break;
         case svIndex:
         {
+            typedef sc::TokenStringContext::IndexNameMapType NameType;
+
             sal_uInt16 nIndex = rToken.GetIndex();
             switch (eOp)
             {
                 case ocName:
                 {
-                    sc::TokenStringContext::IndexNameMapType::const_iterator it = rCxt.maGlobalRangeNames.find(nIndex);
-                    if (it != rCxt.maGlobalRangeNames.end())
+                    if (rToken.IsGlobal())
+                    {
+                        // global named range
+                        NameType::const_iterator it = rCxt.maGlobalRangeNames.find(nIndex);
+                        if (it == rCxt.maGlobalRangeNames.end())
+                        {
+                            rBuf.append(ScGlobal::GetRscString(STR_NO_NAME_REF));
+                            break;
+                        }
+
                         rBuf.append(it->second);
+                    }
+                    else
+                    {
+                        // sheet-local named range
+                        sc::TokenStringContext::TabIndexMapType::const_iterator itTab = rCxt.maSheetRangeNames.find(rPos.Tab());
+                        if (itTab == rCxt.maSheetRangeNames.end())
+                        {
+                            rBuf.append(ScGlobal::GetRscString(STR_NO_NAME_REF));
+                            break;
+                        }
+
+                        const NameType& rNames = itTab->second;
+                        NameType::const_iterator it = rNames.find(nIndex);
+                        if (it == rNames.end())
+                        {
+                            rBuf.append(ScGlobal::GetRscString(STR_NO_NAME_REF));
+                            break;
+                        }
+
+                        rBuf.append(it->second);
+                    }
                 }
                 break;
                 case ocDBArea:
                 {
-                    sc::TokenStringContext::IndexNameMapType::const_iterator it = rCxt.maNamedDBs.find(nIndex);
+                    NameType::const_iterator it = rCxt.maNamedDBs.find(nIndex);
                     if (it != rCxt.maNamedDBs.end())
                         rBuf.append(it->second);
                 }
diff --git a/sc/source/core/tool/tokenstringcontext.cxx b/sc/source/core/tool/tokenstringcontext.cxx
index 6eaf0ee..e3aea0e 100644
--- a/sc/source/core/tool/tokenstringcontext.cxx
+++ b/sc/source/core/tool/tokenstringcontext.cxx
@@ -16,6 +16,21 @@ using namespace com::sun::star;
 
 namespace sc {
 
+namespace {
+
+void insertAllNames( TokenStringContext::IndexNameMapType& rMap, const ScRangeName& rNames )
+{
+    ScRangeName::const_iterator it = rNames.begin(), itEnd = rNames.end();
+    for (; it != itEnd; ++it)
+    {
+        const ScRangeData* pData = it->second;
+        rMap.insert(
+            TokenStringContext::IndexNameMapType::value_type(pData->GetIndex(), pData->GetName()));
+    }
+}
+
+}
+
 TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::FormulaGrammar::Grammar eGram ) :
     meGram(eGram),
     mpRefConv(ScCompiler::GetRefConvention(formula::FormulaGrammar::extractRefConvention(eGram)))
@@ -25,39 +40,50 @@ TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::Formula
     if (mxOpCodeMap)
         maErrRef = mxOpCodeMap->getSymbol(ocErrRef);
 
-    if (pDoc)
+    if (!pDoc)
+        return;
+
+    // Fetch all sheet names.
+    maTabNames = pDoc->GetAllTableNames();
     {
-        // Fetch all sheet names.
-        maTabNames = pDoc->GetAllTableNames();
-        {
-            std::vector<OUString>::iterator it = maTabNames.begin(), itEnd = maTabNames.end();
-            for (; it != itEnd; ++it)
-                ScCompiler::CheckTabQuotes(*it, formula::FormulaGrammar::extractRefConvention(eGram));
-        }
+        std::vector<OUString>::iterator it = maTabNames.begin(), itEnd = maTabNames.end();
+        for (; it != itEnd; ++it)
+            ScCompiler::CheckTabQuotes(*it, formula::FormulaGrammar::extractRefConvention(eGram));
+    }
+
+    // Fetch all named range names.
+    const ScRangeName* pNames = pDoc->GetRangeName();
+    if (pNames)
+        // global names
+        insertAllNames(maGlobalRangeNames, *pNames);
 
-        // Fetch all named range names.
-        const ScRangeName* pNames = pDoc->GetRangeName();
-        if (pNames)
+    {
+        ScRangeName::TabNameCopyMap aTabRangeNames;
+        pDoc->GetAllTabRangeNames(aTabRangeNames);
+        ScRangeName::TabNameCopyMap::const_iterator it = aTabRangeNames.begin(), itEnd = aTabRangeNames.end();
+        for (; it != itEnd; ++it)
         {
-            ScRangeName::const_iterator it = pNames->begin(), itEnd = pNames->end();
-            for (; it != itEnd; ++it)
-            {
-                const ScRangeData* pData = it->second;
-                maGlobalRangeNames.insert(IndexNameMapType::value_type(pData->GetIndex(), pData->GetName()));
-            }
+            const ScRangeName* pSheetNames = it->second;
+            if (!pSheetNames)
+                continue;
+
+            SCTAB nTab = it->first;
+            IndexNameMapType aNames;
+            insertAllNames(aNames, *pSheetNames);
+            maSheetRangeNames.insert(TabIndexMapType::value_type(nTab, aNames));
         }
+    }
 
-        // Fetch all named database ranges names.
-        const ScDBCollection* pDBs = pDoc->GetDBCollection();
-        if (pDBs)
+    // Fetch all named database ranges names.
+    const ScDBCollection* pDBs = pDoc->GetDBCollection();
+    if (pDBs)
+    {
+        const ScDBCollection::NamedDBs& rNamedDBs = pDBs->getNamedDBs();
+        ScDBCollection::NamedDBs::const_iterator it = rNamedDBs.begin(), itEnd = rNamedDBs.end();
+        for (; it != itEnd; ++it)
         {
-            const ScDBCollection::NamedDBs& rNamedDBs = pDBs->getNamedDBs();
-            ScDBCollection::NamedDBs::const_iterator it = rNamedDBs.begin(), itEnd = rNamedDBs.end();
-            for (; it != itEnd; ++it)
-            {
-                const ScDBData& rData = *it;
-                maNamedDBs.insert(IndexNameMapType::value_type(rData.GetIndex(), rData.GetName()));
-            }
+            const ScDBData& rData = *it;
+            maNamedDBs.insert(IndexNameMapType::value_type(rData.GetIndex(), rData.GetName()));
         }
     }
 }
commit 24ddf0d93879cfa0d92b2144685960eed26116fb
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 15:21:18 2013 -0500

    Handle named database ranges in CreateString().
    
    Change-Id: I6904b9de0f7d711252797bb2e33ba4c462476b2d

diff --git a/sc/inc/tokenstringcontext.hxx b/sc/inc/tokenstringcontext.hxx
index 5bcc3b3..6015a59 100644
--- a/sc/inc/tokenstringcontext.hxx
+++ b/sc/inc/tokenstringcontext.hxx
@@ -35,6 +35,7 @@ struct SC_DLLPUBLIC TokenStringContext
 
     std::vector<OUString> maTabNames;
     IndexNameMapType maGlobalRangeNames;
+    IndexNameMapType maNamedDBs;
 
     TokenStringContext( const ScDocument* pDoc, formula::FormulaGrammar::Grammar eGram );
 };
diff --git a/sc/qa/unit/ucalc_formula.cxx b/sc/qa/unit/ucalc_formula.cxx
index 43e0d1d..35a3599 100644
--- a/sc/qa/unit/ucalc_formula.cxx
+++ b/sc/qa/unit/ucalc_formula.cxx
@@ -23,6 +23,7 @@
 #include "docfunc.hxx"
 #include "paramisc.hxx"
 #include "tokenstringcontext.hxx"
+#include "dbdata.hxx"
 
 #include "formula/vectortoken.hxx"
 
@@ -65,6 +66,31 @@ void Test::testFormulaCreateStringFromTokens()
         CPPUNIT_ASSERT_MESSAGE("Failed to insert a new name.", bInserted);
     }
 
+    // Insert DB ranges.
+    struct {
+        const char* pName;
+        SCTAB nTab;
+        SCCOL nCol1;
+        SCROW nRow1;
+        SCCOL nCol2;
+        SCROW nRow2;
+    } aDBs[] = {
+        { "Table1", 0, 0, 0, 10, 10 },
+        { "Table2", 1, 0, 0, 10, 10 },
+        { "Table3", 2, 0, 0, 10, 10 }
+    };
+
+    ScDBCollection* pDBs = m_pDoc->GetDBCollection();
+    CPPUNIT_ASSERT_MESSAGE("Failed to fetch DB collection object.", pDBs);
+
+    for (size_t i = 0, n = SAL_N_ELEMENTS(aDBs); i < n; ++i)
+    {
+        ScDBData* pData = new ScDBData(
+            OUString::createFromAscii(
+                aDBs[i].pName), aDBs[i].nTab, aDBs[i].nCol1, aDBs[i].nRow1, aDBs[i].nCol2,aDBs[i].nRow2);
+        pDBs->getNamedDBs().insert(pData);
+    }
+
     const char* aTests[] = {
         "1+2",
         "SUM(A1:A10;B1:B10;C5;D6)",
@@ -72,7 +98,8 @@ void Test::testFormulaCreateStringFromTokens()
         "AVERAGE('2013'.B10:C20)",
         "'Kevin''s Data'.B10",
         "'Past Data'.B1+'2013'.B2*(1+'Kevin''s Data'.C10)",
-        "x+y*z"
+        "x+y*z", // named ranges
+        "MAX(Table1)+MIN(Table2)*SUM(Table3)" // database ranges
     };
 
     boost::scoped_ptr<ScTokenArray> pArray;
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index 0aeb84c..4966369 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -40,6 +40,7 @@
 #include "refupdatecontext.hxx"
 #include "tokenstringcontext.hxx"
 #include "types.hxx"
+#include "globstr.hrc"
 #include "svl/sharedstring.hxx"
 
 using ::std::vector;
@@ -3273,9 +3274,15 @@ void appendTokenByType( sc::TokenStringContext& rCxt, OUStringBuffer& rBuf, cons
                         rBuf.append(it->second);
                 }
                 break;
-                    // TODO : Handle other name types.
+                case ocDBArea:
+                {
+                    sc::TokenStringContext::IndexNameMapType::const_iterator it = rCxt.maNamedDBs.find(nIndex);
+                    if (it != rCxt.maNamedDBs.end())
+                        rBuf.append(it->second);
+                }
+                break;
                 default:
-                    ;
+                    rBuf.append(ScGlobal::GetRscString(STR_NO_NAME_REF));
             }
         }
         break;
diff --git a/sc/source/core/tool/tokenstringcontext.cxx b/sc/source/core/tool/tokenstringcontext.cxx
index 075bd96..6eaf0ee 100644
--- a/sc/source/core/tool/tokenstringcontext.cxx
+++ b/sc/source/core/tool/tokenstringcontext.cxx
@@ -10,6 +10,7 @@
 #include "tokenstringcontext.hxx"
 #include "compiler.hxx"
 #include "document.hxx"
+#include "dbdata.hxx"
 
 using namespace com::sun::star;
 
@@ -26,6 +27,7 @@ TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::Formula
 
     if (pDoc)
     {
+        // Fetch all sheet names.
         maTabNames = pDoc->GetAllTableNames();
         {
             std::vector<OUString>::iterator it = maTabNames.begin(), itEnd = maTabNames.end();
@@ -33,6 +35,7 @@ TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::Formula
                 ScCompiler::CheckTabQuotes(*it, formula::FormulaGrammar::extractRefConvention(eGram));
         }
 
+        // Fetch all named range names.
         const ScRangeName* pNames = pDoc->GetRangeName();
         if (pNames)
         {
@@ -43,6 +46,19 @@ TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::Formula
                 maGlobalRangeNames.insert(IndexNameMapType::value_type(pData->GetIndex(), pData->GetName()));
             }
         }
+
+        // Fetch all named database ranges names.
+        const ScDBCollection* pDBs = pDoc->GetDBCollection();
+        if (pDBs)
+        {
+            const ScDBCollection::NamedDBs& rNamedDBs = pDBs->getNamedDBs();
+            ScDBCollection::NamedDBs::const_iterator it = rNamedDBs.begin(), itEnd = rNamedDBs.end();
+            for (; it != itEnd; ++it)
+            {
+                const ScDBData& rData = *it;
+                maNamedDBs.insert(IndexNameMapType::value_type(rData.GetIndex(), rData.GetName()));
+            }
+        }
     }
 }
 
commit b73b5750ed51acbb5076bc51159a2d6c87c69cbf
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 12:49:53 2013 -0500

    Pre-quote sheet names to avoid doing it every single time.
    
    Change-Id: Ib103ca22bf59c334945c0462588610899a0acfaa

diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index 395bf11..a9d17d6 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -734,10 +734,7 @@ struct ConventionOOO_A1 : public Convention_A1
         if (static_cast<size_t>(nTab) >= rCxt.maTabNames.size())
             aString = ScGlobal::GetRscString(STR_NO_REF_TABLE);
         else
-        {
             aString = rCxt.maTabNames[nTab];
-            ScCompiler::CheckTabQuotes( aString, FormulaGrammar::CONV_OOO );
-        }
         aString += ".";
         return aString;
     }
@@ -1040,10 +1037,6 @@ struct ConventionXL
             return;
         }
         rTabName = rCxt.maTabNames[aAbs.Tab()];
-
-        // XL uses the same sheet name quoting conventions in both modes
-        // it is safe to use A1 here
-        ScCompiler::CheckTabQuotes( rTabName, FormulaGrammar::CONV_XL_A1 );
     }
 
     static void MakeTabStr( OUStringBuffer& rBuf,
diff --git a/sc/source/core/tool/tokenstringcontext.cxx b/sc/source/core/tool/tokenstringcontext.cxx
index b26d338..075bd96 100644
--- a/sc/source/core/tool/tokenstringcontext.cxx
+++ b/sc/source/core/tool/tokenstringcontext.cxx
@@ -27,6 +27,12 @@ TokenStringContext::TokenStringContext( const ScDocument* pDoc, formula::Formula
     if (pDoc)
     {
         maTabNames = pDoc->GetAllTableNames();
+        {
+            std::vector<OUString>::iterator it = maTabNames.begin(), itEnd = maTabNames.end();
+            for (; it != itEnd; ++it)
+                ScCompiler::CheckTabQuotes(*it, formula::FormulaGrammar::extractRefConvention(eGram));
+        }
+
         const ScRangeName* pNames = pDoc->GetRangeName();
         if (pNames)
         {
commit 2c92ee37abee82b7190c88bd16d4f12356acdf60
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 12:33:41 2013 -0500

    We don't need this any more.
    
    This was used when external references used hidden sheets as a cheesy
    workaround.
    
    Change-Id: I7e4bf3a2235a0c2e80d0aa71a81f22a10b8d0fd5

diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index b5fdedc..395bf11 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -728,7 +728,7 @@ struct ConventionOOO_A1 : public Convention_A1
     ConventionOOO_A1() : Convention_A1 (FormulaGrammar::CONV_OOO) { }
     ConventionOOO_A1( FormulaGrammar::AddressConvention eConv ) : Convention_A1 (eConv) { }
 
-    static OUString MakeTabStr( const sc::TokenStringContext& rCxt, SCTAB nTab, OUString& aDoc )
+    static OUString MakeTabStr( const sc::TokenStringContext& rCxt, SCTAB nTab )
     {
         OUString aString;
         if (static_cast<size_t>(nTab) >= rCxt.maTabNames.size())
@@ -736,17 +736,6 @@ struct ConventionOOO_A1 : public Convention_A1
         else
         {
             aString = rCxt.maTabNames[nTab];
-            // "'Doc'#Tab"
-            sal_Int32 nPos = ScCompiler::GetDocTabPos( aString );
-            if ( nPos != -1 )
-            {
-                aDoc = aString.copy( 0, nPos + 1 );
-                aString = aString.copy( nPos + 1 );
-                aDoc = INetURLObject::decode( aDoc, INET_HEX_ESCAPE,
-                                              INetURLObject::DECODE_UNAMBIGUOUS );
-            }
-            else
-                aDoc = "";
             ScCompiler::CheckTabQuotes( aString, FormulaGrammar::CONV_OOO );
         }
         aString += ".";
@@ -769,9 +758,7 @@ struct ConventionOOO_A1 : public Convention_A1
             }
             else
             {
-                OUString aDoc;
-                OUString aRefStr(MakeTabStr(rCxt, rAbsRef.Tab(), aDoc));
-                rBuffer.append(aDoc);
+                OUString aRefStr(MakeTabStr(rCxt, rAbsRef.Tab()));
                 if (!rRef.IsTabRel())
                     rBuffer.append('$');
                 rBuffer.append(aRefStr);
@@ -1042,40 +1029,24 @@ const ScCompiler::Convention * const ScCompiler::pConvOOO_A1_ODF = &ConvOOO_A1_O
 
 struct ConventionXL
 {
-    static bool GetDocAndTab(
+    static void GetTab(
         const ScAddress& rPos, const sc::TokenStringContext& rCxt,
-        const ScSingleRefData& rRef, OUString& rDocName, OUString& rTabName )
+        const ScSingleRefData& rRef, OUString& rTabName )
     {
-        bool bHasDoc = false;
-
-        rDocName = "";
         ScAddress aAbs = rRef.toAbs(rPos);
         if (rRef.IsTabDeleted() || static_cast<size_t>(aAbs.Tab()) >= rCxt.maTabNames.size())
         {
             rTabName = ScGlobal::GetRscString( STR_NO_REF_TABLE );
-            return false;
+            return;
         }
         rTabName = rCxt.maTabNames[aAbs.Tab()];
 
-        // Cheesy hack to unparse the OOO style "'Doc'#Tab"
-        sal_Int32 nPos = ScCompiler::GetDocTabPos( rTabName);
-        if (nPos != -1)
-        {
-            rDocName = rTabName.copy( 0, nPos );
-            // TODO : More research into how XL escapes the doc path
-            rDocName = INetURLObject::decode( rDocName, INET_HEX_ESCAPE,
-                    INetURLObject::DECODE_UNAMBIGUOUS );
-            rTabName = rTabName.copy( nPos + 1 );
-            bHasDoc = true;
-        }
-
         // XL uses the same sheet name quoting conventions in both modes
         // it is safe to use A1 here
         ScCompiler::CheckTabQuotes( rTabName, FormulaGrammar::CONV_XL_A1 );
-        return bHasDoc;
     }
 
-    static void MakeDocStr( OUStringBuffer& rBuf,
+    static void MakeTabStr( OUStringBuffer& rBuf,
                             const ScAddress& rPos,
                             const sc::TokenStringContext& rCxt,
                             const ScComplexRefData& rRef,
@@ -1083,29 +1054,13 @@ struct ConventionXL
     {
         if( rRef.Ref1.IsFlag3D() )
         {
-            OUString aStartTabName, aStartDocName, aEndTabName, aEndDocName;
-            bool bStartHasDoc = false, bEndHasDoc = false;
+            OUString aStartTabName, aEndTabName;
 
-            bStartHasDoc = GetDocAndTab(
-                rPos, rCxt, rRef.Ref1, aStartDocName, aStartTabName);
+            GetTab(rPos, rCxt, rRef.Ref1, aStartTabName);
 
             if( !bSingleRef && rRef.Ref2.IsFlag3D() )
             {
-                bEndHasDoc = GetDocAndTab(
-                    rPos, rCxt, rRef.Ref2, aEndDocName, aEndTabName);
-            }
-            else
-                bEndHasDoc = bStartHasDoc;
-
-            if( bStartHasDoc )
-            {
-                // A ref across multipled workbooks ?
-                if( !bEndHasDoc )
-                    return;
-
-                rBuf.append( '[' );
-                rBuf.append( aStartDocName );
-                rBuf.append( ']' );
+                GetTab(rPos, rCxt, rRef.Ref2, aEndTabName);
             }
 
             rBuf.append( aStartTabName );
@@ -1274,7 +1229,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         // Foo!A1:#REF! versus #REF! at this point
         ScAddress aAbs1 = aRef.Ref1.toAbs(rPos), aAbs2;
 
-        MakeDocStr(rBuf, rPos, rCxt, aRef, bSingleRef);
+        MakeTabStr(rBuf, rPos, rCxt, aRef, bSingleRef);
 
         if (!ValidAddress(aAbs1))
         {
@@ -1461,7 +1416,7 @@ struct ConventionXL_R1C1 : public ScCompiler::Convention, public ConventionXL
         ScRange aAbsRef = rRef.toAbs(rPos);
         ScComplexRefData aRef( rRef );
 
-        MakeDocStr(rBuf, rPos, rCxt, aRef, bSingleRef);
+        MakeTabStr(rBuf, rPos, rCxt, aRef, bSingleRef);
 
         // Play fast and loose with invalid refs.  There is not much point in producing
         // Foo!A1:#REF! versus #REF! at this point
commit f79537242c069668d4d2fb616e22f00b9b0153ee
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Fri Nov 15 11:28:48 2013 -0500

    Have makeRefStr() accept a context object rather than individual params.
    
    Change-Id: Id52e02aa29d85d207420c80a95ada2f8487f165f

diff --git a/sc/inc/compiler.hxx b/sc/inc/compiler.hxx
index 6844b28..ee3c689 100644
--- a/sc/inc/compiler.hxx
+++ b/sc/inc/compiler.hxx
@@ -78,6 +78,12 @@ class ScRangeData;
 class ScExternalRefManager;
 class ScTokenArray;
 
+namespace sc {
+
+struct TokenStringContext;
+
+}
+
 // constants and data types internal to compiler
 
 /*
@@ -235,8 +241,7 @@ public:
         virtual ~Convention();
 
         virtual void makeRefStr(
-            OUStringBuffer& rBuffer, const ScAddress& rPos, formula::FormulaGrammar::Grammar eGram,
-            const OUString& rErrRef, const std::vector<OUString>& rTabNames,
+            OUStringBuffer& rBuffer, const ScAddress& rPos, const sc::TokenStringContext& rCxt,
             const ScComplexRefData& rRef, bool bSingleRef ) const = 0;
 
         virtual ::com::sun::star::i18n::ParseResult
@@ -333,6 +338,7 @@ private:
     bool        mbCloseBrackets;            // whether to close open brackets automatically, default TRUE
     bool        mbRewind;                   // whether symbol is to be rewound to some step during lexical analysis
     std::vector<sal_uInt16> maExternalFiles;
+    mutable sc::TokenStringContext* mpTokenStringCxt;
 
     bool   NextNewToken(bool bInArray = false);
 
@@ -370,6 +376,8 @@ public:
 
     ScCompiler( ScDocument* pDocument, const ScAddress&,ScTokenArray& rArr);
 
+    virtual ~ScCompiler();
+
 public:
     static void DeInit();               /// all
 
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index 1ef766c..b5fdedc 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -67,6 +67,7 @@
 #include "formulaparserpool.hxx"
 #include "tokenarray.hxx"
 #include "scmatrix.hxx"
+#include "tokenstringcontext.hxx"
 
 using namespace formula;
 using namespace ::com::sun::star;
@@ -727,14 +728,14 @@ struct ConventionOOO_A1 : public Convention_A1
     ConventionOOO_A1() : Convention_A1 (FormulaGrammar::CONV_OOO) { }
     ConventionOOO_A1( FormulaGrammar::AddressConvention eConv ) : Convention_A1 (eConv) { }
 
-    static OUString MakeTabStr( const std::vector<OUString>& rTabNames, SCTAB nTab, OUString& aDoc )
+    static OUString MakeTabStr( const sc::TokenStringContext& rCxt, SCTAB nTab, OUString& aDoc )
     {
         OUString aString;
-        if (static_cast<size_t>(nTab) >= rTabNames.size())
+        if (static_cast<size_t>(nTab) >= rCxt.maTabNames.size())
             aString = ScGlobal::GetRscString(STR_NO_REF_TABLE);
         else
         {
-            aString = rTabNames[nTab];
+            aString = rCxt.maTabNames[nTab];
             // "'Doc'#Tab"
             sal_Int32 nPos = ScCompiler::GetDocTabPos( aString );
             if ( nPos != -1 )
@@ -753,7 +754,7 @@ struct ConventionOOO_A1 : public Convention_A1
     }
 
     void MakeOneRefStrImpl(
-        OUStringBuffer& rBuffer, const OUString& rErrRef, const std::vector<OUString>& rTabNames,
+        OUStringBuffer& rBuffer, const sc::TokenStringContext& rCxt,
         const ScSingleRefData& rRef, const ScAddress& rAbsRef,
         bool bForceTab, bool bODF ) const
     {
@@ -763,13 +764,13 @@ struct ConventionOOO_A1 : public Convention_A1
             {
                 if (!rRef.IsTabRel())
                     rBuffer.append('$');
-                rBuffer.append(rErrRef);
+                rBuffer.append(rCxt.maErrRef);
                 rBuffer.append('.');
             }
             else
             {
                 OUString aDoc;
-                OUString aRefStr(MakeTabStr(rTabNames, rAbsRef.Tab(), aDoc));
+                OUString aRefStr(MakeTabStr(rCxt, rAbsRef.Tab(), aDoc));
                 rBuffer.append(aDoc);
                 if (!rRef.IsTabRel())
                     rBuffer.append('$');
@@ -781,22 +782,20 @@ struct ConventionOOO_A1 : public Convention_A1
         if (!rRef.IsColRel())
             rBuffer.append('$');
         if (!ValidCol(rAbsRef.Col()))
-            rBuffer.append(rErrRef);
+            rBuffer.append(rCxt.maErrRef);
         else
             MakeColStr(rBuffer, rAbsRef.Col());
         if (!rRef.IsRowRel())
             rBuffer.append('$');
         if (!ValidRow(rAbsRef.Row()))
-            rBuffer.append(rErrRef);
+            rBuffer.append(rCxt.maErrRef);
         else
             MakeRowStr(rBuffer, rAbsRef.Row());
     }
 
     void makeRefStr( OUStringBuffer&   rBuffer,
                      const ScAddress& rPos,
-                     FormulaGrammar::Grammar /*eGram*/,
-                     const OUString& rErrRef,
-                     const std::vector<OUString>& rTabNames,
+                     const sc::TokenStringContext& rCxt,
                      const ScComplexRefData& rRef,
                      bool bSingleRef ) const
     {
@@ -807,11 +806,11 @@ struct ConventionOOO_A1 : public Convention_A1
         if( !bSingleRef )
             aAbs2 = aRef.Ref2.toAbs(rPos);
 
-        MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref1, aAbs1, false, false);
+        MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref1, aAbs1, false, false);
         if (!bSingleRef)
         {
             rBuffer.append(':');
-            MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), false);
+            MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), false);
         }
     }
 
@@ -987,9 +986,7 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
     ConventionOOO_A1_ODF() : ConventionOOO_A1 (FormulaGrammar::CONV_ODF) { }
     void makeRefStr( OUStringBuffer&   rBuffer,
                      const ScAddress& rPos,
-                     FormulaGrammar::Grammar eGram,
-                     const OUString& rErrRef,
-                     const std::vector<OUString>& rTabNames,
+                     const sc::TokenStringContext& rCxt,
                      const ScComplexRefData& rRef,
                      bool bSingleRef ) const
     {
@@ -1001,20 +998,20 @@ struct ConventionOOO_A1_ODF : public ConventionOOO_A1
         if( !bSingleRef )
             aAbs2 = aRef.Ref2.toAbs(rPos);
 
-        if (FormulaGrammar::isODFF(eGram) && (!ValidAddress(aAbs1) || !ValidAddress(aAbs2)))
+        if (FormulaGrammar::isODFF(rCxt.meGram) && (!ValidAddress(aAbs1) || !ValidAddress(aAbs2)))
         {
-            rBuffer.append(rErrRef);
+            rBuffer.append(rCxt.maErrRef);
             // For ODFF write [#REF!], but not for PODF so apps reading ODF
             // 1.0/1.1 may have a better chance if they implemented the old
             // form.
         }
         else
         {
-            MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref1, aAbs1, false, true);
+            MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref1, aAbs1, false, true);
             if (!bSingleRef)
             {
                 rBuffer.append(sal_Unicode(':'));
-                MakeOneRefStrImpl(rBuffer, rErrRef, rTabNames, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), true);
+                MakeOneRefStrImpl(rBuffer, rCxt, aRef.Ref2, aAbs2, aAbs1.Tab() != aAbs2.Tab(), true);
             }
         }
         rBuffer.append(sal_Unicode(']'));
@@ -1046,19 +1043,19 @@ const ScCompiler::Convention * const ScCompiler::pConvOOO_A1_ODF = &ConvOOO_A1_O
 struct ConventionXL
 {
     static bool GetDocAndTab(
-        const ScAddress& rPos, const std::vector<OUString>& rTabNames,
+        const ScAddress& rPos, const sc::TokenStringContext& rCxt,
         const ScSingleRefData& rRef, OUString& rDocName, OUString& rTabName )
     {
         bool bHasDoc = false;
 
         rDocName = "";
         ScAddress aAbs = rRef.toAbs(rPos);
-        if (rRef.IsTabDeleted() || static_cast<size_t>(aAbs.Tab()) >= rTabNames.size())
+        if (rRef.IsTabDeleted() || static_cast<size_t>(aAbs.Tab()) >= rCxt.maTabNames.size())
         {
             rTabName = ScGlobal::GetRscString( STR_NO_REF_TABLE );
             return false;
         }
-        rTabName = rTabNames[aAbs.Tab()];
+        rTabName = rCxt.maTabNames[aAbs.Tab()];
 
         // Cheesy hack to unparse the OOO style "'Doc'#Tab"
         sal_Int32 nPos = ScCompiler::GetDocTabPos( rTabName);
@@ -1080,7 +1077,7 @@ struct ConventionXL
 
     static void MakeDocStr( OUStringBuffer& rBuf,
                             const ScAddress& rPos,
-                            const std::vector<OUString>& rTabNames,
+                            const sc::TokenStringContext& rCxt,
                             const ScComplexRefData& rRef,
                             bool bSingleRef )
     {
@@ -1090,12 +1087,12 @@ struct ConventionXL
             bool bStartHasDoc = false, bEndHasDoc = false;
 
             bStartHasDoc = GetDocAndTab(
-                rPos, rTabNames, rRef.Ref1, aStartDocName, aStartTabName);
+                rPos, rCxt, rRef.Ref1, aStartDocName, aStartTabName);
 
             if( !bSingleRef && rRef.Ref2.IsFlag3D() )
             {
                 bEndHasDoc = GetDocAndTab(
-                    rPos, rTabNames, rRef.Ref2, aEndDocName, aEndTabName);
+                    rPos, rCxt, rRef.Ref2, aEndDocName, aEndTabName);
             }
             else
                 bEndHasDoc = bStartHasDoc;
@@ -1267,9 +1264,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
 
     void makeRefStr( OUStringBuffer&   rBuf,
                      const ScAddress& rPos,
-                     FormulaGrammar::Grammar /*eGram*/,
-                     const OUString& /*rErrRef*/,
-                     const std::vector<OUString>& rTabNames,
+                     const sc::TokenStringContext& rCxt,
                      const ScComplexRefData& rRef,
                      bool bSingleRef ) const
     {
@@ -1279,7 +1274,7 @@ struct ConventionXL_A1 : public Convention_A1, public ConventionXL
         // Foo!A1:#REF! versus #REF! at this point
         ScAddress aAbs1 = aRef.Ref1.toAbs(rPos), aAbs2;
 
-        MakeDocStr(rBuf, rPos, rTabNames, aRef, bSingleRef);
+        MakeDocStr(rBuf, rPos, rCxt, aRef, bSingleRef);
 
         if (!ValidAddress(aAbs1))
         {

... etc. - the rest is truncated


More information about the Libreoffice-commits mailing list