]> git.lyx.org Git - features.git/commitdiff
Fix wrong mode on output for macros that shadow global macros
authorGuillaume Munch <gm@lyx.org>
Mon, 12 Dec 2016 00:17:41 +0000 (01:17 +0100)
committerGuillaume Munch <gm@lyx.org>
Tue, 3 Jan 2017 19:35:20 +0000 (20:35 +0100)
Testcase: Define a math macro \AA, overriding the definition of \AA from
lib/symbols, then insert it in math mode.

* Before this commit: \text{\AA}, and \lyxmathsym{\AA} after deleting \text, but
  displayed like \AA.

* After this commit: \text{\AA} is inserted, but one gets \AA after deleting
  \text. The output is now consistent with the display and the meaning.

* Expected: only \AA is inserted. This is unfortuately not what one gets; for
  this to work, the scope of the macros would need to be resolved upon creating
  the inset.

src/mathed/MathMacro.cpp
src/mathed/MathMacro.h

index 7862962bfe7e02bc5ea5670144ed761d858d9e95..da22747ed881c7dce3da916ba727140e2eb53672 100644 (file)
@@ -823,12 +823,34 @@ size_t MathMacro::appetite() const
 
 InsetMath::mode_type MathMacro::currentMode() const
 {
-       // User defined macros are always assumed to be mathmode macros.
-       // Only the global macros defined in lib/symbols may be textmode.
+       // There is no way to guess the mode of user defined macros, so they are
+       // always assumed to be mathmode.  Only the global macros defined in
+       // lib/symbols may be textmode.
+       mode_type mode = modeToEnsure();
+       return (mode == UNDECIDED_MODE) ? MATH_MODE : mode;
+}
+
+
+InsetMath::mode_type MathMacro::modeToEnsure() const
+{
+       // User defined macros can be either text mode or math mode for output and
+       // display. There is no way to guess. For global macros defined in
+       // lib/symbols, we ensure textmode if flagged as such, otherwise we ensure
+       // math mode.
+       if (MacroData const * m = macroBackup())
+               if (m->symbol())
+                       return (m->symbol()->extra == "textmode") ? TEXT_MODE : MATH_MODE;
+       return UNDECIDED_MODE;
+}
+
 
-       MacroData const * data = MacroTable::globalMacros().get(name());
-       bool textmode = data && data->symbol() && data->symbol()->extra == "textmode";
-       return textmode ? TEXT_MODE : MATH_MODE;
+MacroData const * MathMacro::macroBackup() const
+{
+       if (macro())
+               return &d->macroBackup_;
+       if (MacroData const * data = MacroTable::globalMacros().get(name()))
+               return data;
+       return nullptr;
 }
 
 
@@ -1009,12 +1031,9 @@ bool MathMacro::folded() const
 
 void MathMacro::write(WriteStream & os) const
 {
-       MacroData const * data = MacroTable::globalMacros().get(name());
-       bool textmode_macro = data && data->symbol()
-                                  && data->symbol()->extra == "textmode";
-       bool needs_mathmode = data && (!data->symbol()
-                                      || data->symbol()->extra != "textmode");
-
+       mode_type mode = modeToEnsure();
+       bool textmode_macro = mode == TEXT_MODE;
+       bool needs_mathmode = mode == MATH_MODE;
        MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
 
        // non-normal mode
index 157e6e64efb3f04bf5e8eb01ef852fa5acf7b9a9..5584dd70a5a2899186f296affa335c93bcfb4240 100644 (file)
@@ -122,7 +122,7 @@ public:
 
        ///
        docstring name() const;
-       ///
+       /// FIXME: Often dangling.
        MacroData const * macro() const;
        ///
        docstring macroName() const;
@@ -164,6 +164,14 @@ protected:
        void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
 
 private:
+       /// Math mode for output and display. UNDECIDED for user macros: they could
+       /// be either.
+       mode_type modeToEnsure() const;
+       /// This function is needed for now because of two shortfalls of the current
+       /// implementation: the macro() pointer is often dangling, in which case we
+       /// fall back to a backup copy, and the macro is not known at inset
+       /// creation, in which case we fall back to the global macro with this name.
+       MacroData const * macroBackup() const;
        ///
        virtual Inset * clone() const;
        ///