On Thu, Aug 16, 2007 at 03:58:44PM +0200, Jean-Marc Lasgouttes wrote: > Martin Vermeer <[EMAIL PROTECTED]> writes: > > > Sorry... I don't understand this. What do you mean? What > > code does this require? > > Something like that (untested) > > InsetLayout const & TextClass::insetlayout(docstring const & name) const > { > docstring n = name; > while (!n.empty()) { > if (insetlayoutlist_.count(n) > 0) > return insetlayoutlist_[n]; > docstring::size_type i = n.rfind(':'); > if (i != string::npos) // delimiter was found > n = n.substr(0, i); > else > break; > } > static const InsetLayout empty; > return empty; > } > > Assuming that you ask for "charstyle:author", which does not exist, > the code will next try to find an entry for "charstyle" (by removing > the last part), which could be set to > > InsetLayout charstyle > LabelString "Error!" > LatexType command > LatexName "" > LabelFont > Color Red > EndFont > End > > This gives us for free the handling of unknown charstyles. > > Is it clearer now?
OK, attached the patch containing this and a facility for keeping the "leaf" of the charstyle name for display in the label. Just cosmetic. I will commit this if I don't get any objections - Martin
Index: TextClass.cpp =================================================================== --- TextClass.cpp (revision 19610) +++ TextClass.cpp (working copy) @@ -149,7 +149,6 @@ TC_INPUT, TC_STYLE, TC_DEFAULTSTYLE, - TC_CHARSTYLE, TC_INSETLAYOUT, TC_ENVIRONMENT, TC_NOSTYLE, @@ -183,7 +182,6 @@ } keyword_item textClassTags[] = { - { "charstyle", TC_CHARSTYLE }, { "classoptions", TC_CLASSOPTIONS }, { "columns", TC_COLUMNS }, { "counter", TC_COUNTER }, @@ -407,12 +405,6 @@ if (lexrc.next()) rightmargin_ = lexrc.getString(); break; - case TC_CHARSTYLE: - if (lexrc.next()) { - string const name = subst(lexrc.getString(), '_', ' '); - readCharStyle(lexrc, name); - } - break; case TC_INSETLAYOUT: if (lexrc.next()) { docstring const name = subst(lexrc.getDocString(), '_', ' '); @@ -603,19 +595,10 @@ lexrc.popTable(); } -enum CharStyleTags { - CS_FONT = 1, - CS_LABELFONT, - CS_LATEXTYPE, - CS_LATEXNAME, - CS_LATEXPARAM, - CS_PREAMBLE, - CS_END -}; - enum InsetLayoutTags { IL_FONT = 1, + IL_LYXTYPE, IL_LABELFONT, IL_LABELSTRING, IL_LATEXTYPE, @@ -626,84 +609,6 @@ }; - -void TextClass::readCharStyle(Lexer & lexrc, string const & name) -{ - keyword_item elementTags[] = { - { "end", CS_END }, - { "font", CS_FONT }, - { "labelfont", CS_LABELFONT }, - { "latexname", CS_LATEXNAME }, - { "latexparam", CS_LATEXPARAM }, - { "latextype", CS_LATEXTYPE }, - { "preamble", CS_PREAMBLE} - }; - - lexrc.pushTable(elementTags, CS_END); - - string latextype; - string latexname; - string latexparam; - Font font(Font::ALL_INHERIT); - Font labelfont(Font::ALL_INHERIT); - string preamble; - - bool getout = false; - while (!getout && lexrc.isOK()) { - int le = lexrc.lex(); - switch (le) { - case Lexer::LEX_UNDEF: - lexrc.printError("Unknown ClassOption tag `$$Token'"); - continue; - default: break; - } - switch (static_cast<CharStyleTags>(le)) { - case CS_LATEXTYPE: - lexrc.next(); - latextype = lexrc.getString(); - break; - case CS_LATEXNAME: - lexrc.next(); - latexname = lexrc.getString(); - break; - case CS_LATEXPARAM: - lexrc.next(); - latexparam = subst(lexrc.getString(), """, "\""); - break; - case CS_LABELFONT: - labelfont.lyxRead(lexrc); - break; - case CS_FONT: - font.lyxRead(lexrc); - labelfont = font; - break; - case CS_PREAMBLE: - preamble = lexrc.getLongString("EndPreamble"); - break; - case CS_END: - getout = true; - break; - } - } - - // - // Here add element to list if getout == true - if (getout) { - CharStyle cs; - cs.name = name; - cs.latextype = latextype; - cs.latexname = latexname; - cs.latexparam = latexparam; - cs.font = font; - cs.labelfont = labelfont; - cs.preamble = from_utf8(preamble); - charstyles().push_back(cs); - } - - lexrc.popTable(); -} - - void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name) { keyword_item elementTags[] = { @@ -714,11 +619,13 @@ { "latexname", IL_LATEXNAME }, { "latexparam", IL_LATEXPARAM }, { "latextype", IL_LATEXTYPE }, + { "lyxtype", IL_LYXTYPE }, { "preamble", IL_PREAMBLE} }; lexrc.pushTable(elementTags, IL_END); + string lyxtype; docstring labelstring; string latextype; string latexname; @@ -737,6 +644,10 @@ default: break; } switch (static_cast<InsetLayoutTags>(le)) { + case IL_LYXTYPE: + lexrc.next(); + lyxtype = lexrc.getString(); + break; case IL_LATEXTYPE: lexrc.next(); latextype = lexrc.getString(); @@ -775,6 +686,8 @@ // Here add element to list if getout == true if (getout) { InsetLayout il; + il.name = to_ascii(name); + il.lyxtype = lyxtype; il.labelstring = labelstring; il.latextype = latextype; il.latexname = latexname; @@ -783,6 +696,11 @@ il.labelfont = labelfont; il.preamble = from_utf8(preamble); insetlayoutlist_[name] = il; + + // test name for CS: + if (il.lyxtype == "charstyle") { + charstyles().push_back(il); + } } lexrc.popTable(); @@ -1095,7 +1013,16 @@ InsetLayout const & TextClass::insetlayout(docstring const & name) const { - return insetlayoutlist_[name]; + docstring n = name; + while (!n.empty()) { + if (!insetlayoutlist_.count(n) > 0) + return insetlayoutlist_[n]; + docstring::size_type i = n.find(':'); + if (i == string::npos) + n = n.substr(0,i); + } + static const InsetLayout empty; + return empty; } Index: insets/InsetCharStyle.cpp =================================================================== --- insets/InsetCharStyle.cpp (revision 19610) +++ insets/InsetCharStyle.cpp (working copy) @@ -158,6 +158,9 @@ docstring s(from_utf8(params_.type)); if (undefined()) s = _("Undef: ") + s; + // Chop off prefix: + if (s.find(':') != string::npos) + s = s.substr(s.find(':')); theFontMetrics(font).rectText(s, w, a, d); dim.wid = max(dim.wid, w); } @@ -207,6 +210,9 @@ docstring s(from_utf8(params_.type)); if (undefined()) s = _("Undef: ") + s; + // Chop off prefix: + if (s.find(':') != string::npos) + s = s.substr(s.find(':')); theFontMetrics(font).rectText(s, w, a, d); pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a, s, font, Color::none, Color::none); Index: TextClass.h =================================================================== --- TextClass.h (revision 19610) +++ TextClass.h (working copy) @@ -29,20 +29,10 @@ /// -class CharStyle { -public: - std::string name; - std::string latextype; - std::string latexname; - std::string latexparam; - Font font; - Font labelfont; - docstring preamble; -}; - - class InsetLayout { public: + std::string name; + std::string lyxtype; docstring labelstring; std::string latextype; std::string latexname; @@ -54,7 +44,7 @@ /// List of semantically defined character style insets -typedef std::vector<CharStyle> CharStyles; +typedef std::vector<InsetLayout> CharStyles; /// List of inset layouts typedef std::map<docstring, InsetLayout> InsetLayouts;