#include "InsetMathMacro.h"
#include "InsetMathChar.h"
+#include "InsetMathScript.h"
#include "MathCompletionList.h"
#include "MathExtern.h"
#include "MathFactory.h"
return false;*/
// valid characters?
- for (size_t i = 0; i<n.size(); ++i) {
- if (!(n[i] >= 'a' && n[i] <= 'z')
- && !(n[i] >= 'A' && n[i] <= 'Z')
- && n[i] != '*')
- return false;
+ if (n.size() > 1) {
+ for (size_t i = 0; i<n.size(); ++i) {
+ if (!(n[i] >= 'a' && n[i] <= 'z')
+ && !(n[i] >= 'A' && n[i] <= 'Z')
+ && n[i] != '*')
+ return false;
+ }
}
return true;
InsetMath::mode_type InsetMathMacro::currentMode() const
{
- // 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 InsetMathMacro::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;
+ // User defined macros are always assumed to be mathmode macros.
+ // Only the global macros defined in lib/symbols may be textmode.
+ if (MacroData const * m = macroBackup()) {
+ if (m->symbol() && m->symbol()->extra == "textmode")
+ return TEXT_MODE;
+ else
+ return MATH_MODE;
+ }
+ // Unknown macros are undecided.
return UNDECIDED_MODE;
}
void InsetMathMacro::write(WriteStream & os) const
{
- mode_type mode = modeToEnsure();
- bool textmode_macro = mode == TEXT_MODE;
- bool needs_mathmode = mode == MATH_MODE;
- MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
+ mode_type mode = currentMode();
+ MathEnsurer ensurer(os, mode == MATH_MODE, true, mode == TEXT_MODE);
// non-normal mode
if (d->displayMode_ != DISPLAY_NORMAL) {
// print out optionals
for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
first = false;
- os << "[" << cell(i) << "]";
+ // For correctly parsing it when a document is reloaded, we
+ // need to enclose an optional argument in braces if it starts
+ // with a script inset with empty nucleus or ends with a
+ // delimiter-size-modifier macro (see #10497 and #11346)
+ bool braced = false;
+ size_type last = cell(i).size() - 1;
+ if (cell(i).size() && cell(i)[last].nucleus()->asUnknownInset()) {
+ latexkeys const * l = in_word_set(cell(i)[last].nucleus()->name());
+ braced = (l && l->inset == "big");
+ } else if (cell(i).size() && cell(i)[0].nucleus()->asScriptInset()) {
+ braced = cell(i)[0].nucleus()->asScriptInset()->nuc().empty();
+ }
+ if (braced)
+ os << "[{" << cell(i) << "}]";
+ else
+ os << "[" << cell(i) << "]";
}
// skip the tailing empty optionals
}
+void InsetMathMacro::setBuffer(Buffer & buffer)
+{
+ d->definition_.setBuffer(buffer);
+ InsetMathNest::setBuffer(buffer);
+}
+
} // namespace lyx