From e158e07c29a2066ac408d1b0038382623180da9c Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Fri, 19 Oct 2007 17:22:55 +0000 Subject: [PATCH] InsetCommandParams() now takes an InsetCode rather than a string. These changes are just adaptations to the new signature. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21072 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/LyXFunc.cpp | 8 +- src/Paragraph.cpp | 2 +- src/Text3.cpp | 4 +- src/factory.cpp | 205 +++++++++++++++--------------- src/frontends/qt4/GuiDialog.cpp | 2 +- src/frontends/qt4/GuiInclude.cpp | 2 +- src/frontends/qt4/GuiRef.cpp | 2 +- src/frontends/qt4/GuiToc.cpp | 2 +- src/insets/Inset.cpp | 14 ++ src/insets/InsetBibitem.cpp | 2 +- src/insets/InsetBibtex.cpp | 2 +- src/insets/InsetCitation.h | 3 +- src/insets/InsetCommand.cpp | 4 +- src/insets/InsetCommandParams.cpp | 181 ++++++++++++-------------- src/insets/InsetCommandParams.h | 26 ++-- src/insets/InsetFloatList.cpp | 4 +- src/insets/InsetHFill.cpp | 2 +- src/insets/InsetInclude.cpp | 2 +- src/insets/InsetLabel.cpp | 2 +- src/insets/InsetRef.cpp | 2 +- src/mathed/InsetMathHull.cpp | 4 +- src/mathed/InsetMathRef.cpp | 2 +- src/mathed/MathFactory.cpp | 2 +- 23 files changed, 241 insertions(+), 238 deletions(-) diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index 4039cb35ce..ffcb368da6 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -1409,7 +1409,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) case REF_CODE: case TOC_CODE: case HYPERLINK_CODE: { - InsetCommandParams p(name); + InsetCommandParams p(code); data = InsetCommandMailer::params2string(name, p); break; } @@ -1419,7 +1419,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) if (data.empty()) // default type is requested data = "include"; - InsetCommandParams p("include", data); + InsetCommandParams p(INCLUDE_CODE, data); data = InsetIncludeMailer::params2string(p); break; } @@ -1435,7 +1435,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) break; } case CITE_CODE: { - InsetCommandParams p("cite"); + InsetCommandParams p(CITE_CODE); data = InsetCommandMailer::params2string(name, p); break; } @@ -1539,7 +1539,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) arg = token(argument, '|', 0); opt1 = token(argument, '|', 1); } - InsetCommandParams icp("cite"); + InsetCommandParams icp(CITE_CODE); icp["key"] = from_utf8(arg); if (!opt1.empty()) icp["before"] = from_utf8(opt1); diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index f20a92bd6d..35fd0d7caa 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -2432,7 +2432,7 @@ int Paragraph::checkBiblio(bool track_changes) //There was no inset at the beginning, so we need to create one with //the key and label of the one we erased. - InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem"))); + InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE))); // restore values of previously deleted item in this par. if (!oldkey.empty()) inset->setParam("key", oldkey); diff --git a/src/Text3.cpp b/src/Text3.cpp index 726b65b8a9..a6a6820ba5 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -1060,7 +1060,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd) } case LFUN_HYPERLINK_INSERT: { - InsetCommandParams p("href"); + InsetCommandParams p(HYPERLINK_CODE); docstring content; if (cur.selection()) { content = cur.selectionAsString(false); @@ -1079,7 +1079,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd) } case LFUN_LABEL_INSERT: { - InsetCommandParams p("label"); + InsetCommandParams p(LABEL_CODE); // Try to generate a valid label p["name"] = (cmd.argument().empty()) ? cur.getPossibleLabel() : diff --git a/src/factory.cpp b/src/factory.cpp index 61887ed348..557b2be276 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -147,7 +147,7 @@ Inset * createInset(BufferView * bv, FuncRequest const & cmd) return new InsetOptArg(params); case LFUN_BIBITEM_INSERT: - return new InsetBibitem(InsetCommandParams("bibitem")); + return new InsetBibitem(InsetCommandParams(BIBITEM_CODE)); case LFUN_FLOAT_INSERT: { // check if the float type exists @@ -182,7 +182,7 @@ Inset * createInset(BufferView * bv, FuncRequest const & cmd) return new InsetIndex(params); case LFUN_NOMENCL_INSERT: { - InsetCommandParams icp("nomenclature"); + InsetCommandParams icp(NOMENCL_CODE); icp["symbol"] = cmd.argument().empty() ? bv->cursor().innerText()->getStringToIndex(bv->cursor()) : cmd.argument(); @@ -211,13 +211,13 @@ Inset * createInset(BufferView * bv, FuncRequest const & cmd) } case LFUN_INDEX_PRINT: - return new InsetPrintIndex(InsetCommandParams("index_print")); + return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE)); case LFUN_NOMENCL_PRINT: - return new InsetPrintNomencl(InsetCommandParams("nomencl_print")); + return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE)); case LFUN_TOC_INSERT: - return new InsetTOC(InsetCommandParams("toc")); + return new InsetTOC(InsetCommandParams(TOC_CODE)); case LFUN_ENVIRONMENT_INSERT: return new InsetEnvironment(params, cmd.argument()); @@ -237,103 +237,104 @@ Inset * createInset(BufferView * bv, FuncRequest const & cmd) lyxerr << "No such inset '" << name << "'."; return 0; - case BIBITEM_CODE: { - InsetCommandParams icp(name); - InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); - return new InsetBibitem(icp); - } - - case BIBTEX_CODE: { - InsetCommandParams icp(name); - InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); - return new InsetBibtex(icp); - } - - case CITE_CODE: { - InsetCommandParams icp("citation"); - InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); - return new InsetCitation(icp); - } - - case ERT_CODE: { - InsetCollapsable::CollapseStatus st; - InsetERTMailer::string2params(to_utf8(cmd.argument()), st); - return new InsetERT(params, st); - } - - case LISTINGS_CODE: { - InsetListingsParams par; - InsetListingsMailer::string2params(to_utf8(cmd.argument()), par); - return new InsetListings(params, par); - } - - case EXTERNAL_CODE: { - Buffer const & buffer = bv->buffer(); - InsetExternalParams iep; - InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, iep); - auto_ptr inset(new InsetExternal); - inset->setParams(iep, buffer); - return inset.release(); - } - - case GRAPHICS_CODE: { - Buffer const & buffer = bv->buffer(); - InsetGraphicsParams igp; - InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, igp); - auto_ptr inset(new InsetGraphics); - inset->setParams(igp); - return inset.release(); - } - - case HYPERLINK_CODE: { - InsetCommandParams icp(name); - InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); - return new InsetHyperlink(icp); - } - - case INCLUDE_CODE: { - InsetCommandParams iip(name); - InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip); - return new InsetInclude(iip); - } - + case BIBITEM_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); + return new InsetBibitem(icp); + } + + case BIBTEX_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); + return new InsetBibtex(icp); + } + + case CITE_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); + return new InsetCitation(icp); + } + + case ERT_CODE: { + InsetCollapsable::CollapseStatus st; + InsetERTMailer::string2params(to_utf8(cmd.argument()), st); + return new InsetERT(params, st); + } + + case LISTINGS_CODE: { + InsetListingsParams par; + InsetListingsMailer::string2params(to_utf8(cmd.argument()), par); + return new InsetListings(params, par); + } + + case EXTERNAL_CODE: { + Buffer const & buffer = bv->buffer(); + InsetExternalParams iep; + InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, iep); + auto_ptr inset(new InsetExternal); + inset->setParams(iep, buffer); + return inset.release(); + } + + case GRAPHICS_CODE: { + Buffer const & buffer = bv->buffer(); + InsetGraphicsParams igp; + InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, igp); + auto_ptr inset(new InsetGraphics); + inset->setParams(igp); + return inset.release(); + } + + case HYPERLINK_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); + return new InsetHyperlink(icp); + } + + case INCLUDE_CODE: { + InsetCommandParams iip(code); + InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip); + return new InsetInclude(iip); + } + case INDEX_CODE: return new InsetIndex(params); - - case NOMENCL_CODE: { - InsetCommandParams icp(name); - InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp); - return new InsetNomencl(icp); - } - - case LABEL_CODE: { - InsetCommandParams icp(name); - InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); - return new InsetLabel(icp); - } - - case REF_CODE: { - InsetCommandParams icp(name); - InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); - return new InsetRef(icp, bv->buffer()); - } - - case TOC_CODE: { - InsetCommandParams icp("toc"); - InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); - return new InsetTOC(icp); - } - - case VSPACE_CODE: { - VSpace vspace; - InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace); - return new InsetVSpace(vspace); - } - - default: - lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT." - << std::endl; - return 0; + + case NOMENCL_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp); + return new InsetNomencl(icp); + } + + case LABEL_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); + return new InsetLabel(icp); + } + + case REF_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); + return new InsetRef(icp, bv->buffer()); + } + + case TOC_CODE: { + InsetCommandParams icp(code); + InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp); + return new InsetTOC(icp); + } + + case VSPACE_CODE: { + VSpace vspace; + InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace); + return new InsetVSpace(vspace); + } + + default: + lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT." + << std::endl; + return 0; + } } //end LFUN_INSET_INSERT @@ -417,7 +418,7 @@ Inset * readInset(Lexer & lex, Buffer const & buf) //we do not know in advance that we're dealing with a command inset. //Worst case, we could put it in each case below. Better, we could //pass the lexer to the constructor and let the params be built there. - InsetCommandParams inscmd(insetType); + InsetCommandParams inscmd(code); inscmd.read(lex); switch (code) { @@ -499,7 +500,7 @@ Inset * readInset(Lexer & lex, Buffer const & buf) InsetBranchParams())); } else if (tmptok == "Include") { //FIXME - InsetCommandParams p("include"); + InsetCommandParams p(INCLUDE_CODE); inset.reset(new InsetInclude(p)); } else if (tmptok == "Environment") { lex.next(); diff --git a/src/frontends/qt4/GuiDialog.cpp b/src/frontends/qt4/GuiDialog.cpp index b650cda38d..f18d034142 100644 --- a/src/frontends/qt4/GuiDialog.cpp +++ b/src/frontends/qt4/GuiDialog.cpp @@ -270,7 +270,7 @@ namespace lyx { namespace frontend { GuiCommand::GuiCommand(LyXView & lv, string const & name) - : GuiDialog(lv, name), params_(name), lfun_name_(name) + : GuiDialog(lv, name), params_(insetCode(name)), lfun_name_(name) { } diff --git a/src/frontends/qt4/GuiInclude.cpp b/src/frontends/qt4/GuiInclude.cpp index 91fc03bab6..683421faa5 100644 --- a/src/frontends/qt4/GuiInclude.cpp +++ b/src/frontends/qt4/GuiInclude.cpp @@ -60,7 +60,7 @@ using support::getVectorFromString; GuiInclude::GuiInclude(LyXView & lv) - : GuiDialog(lv, "include"), params_("include") + : GuiDialog(lv, "include"), params_(INCLUDE_CODE) { setupUi(this); setViewTitle(_("Child Document")); diff --git a/src/frontends/qt4/GuiRef.cpp b/src/frontends/qt4/GuiRef.cpp index 4da6c34654..7a7a7353f5 100644 --- a/src/frontends/qt4/GuiRef.cpp +++ b/src/frontends/qt4/GuiRef.cpp @@ -52,7 +52,7 @@ using support::makeDisplayPath; static std::string const lfun_name_ = "ref"; GuiRef::GuiRef(LyXView & lv) - : GuiDialog(lv, "ref"), params_("ref") + : GuiDialog(lv, "ref"), params_(REF_CODE) { setupUi(this); setViewTitle(_("Cross-reference")); diff --git a/src/frontends/qt4/GuiToc.cpp b/src/frontends/qt4/GuiToc.cpp index 47c67604cb..6d5a69d406 100644 --- a/src/frontends/qt4/GuiToc.cpp +++ b/src/frontends/qt4/GuiToc.cpp @@ -44,7 +44,7 @@ namespace lyx { namespace frontend { GuiToc::GuiToc(GuiViewBase & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags) - : DockView(parent, "toc", area, flags), params_("toc") + : DockView(parent, "toc", area, flags), params_(TOC_CODE) { widget_ = new TocWidget(*this); setWidget(widget_); diff --git a/src/insets/Inset.cpp b/src/insets/Inset.cpp index 271facdf37..a3f93bd88e 100644 --- a/src/insets/Inset.cpp +++ b/src/insets/Inset.cpp @@ -139,6 +139,20 @@ InsetCode insetCode(std::string const & name) } +std::string insetName(InsetCode c) +{ + static TranslatorMap const translator = build_translator(); + + TranslatorMap::const_iterator it = translator.begin(); + TranslatorMap::const_iterator end = translator.end(); + for (; it != end; ++it) { + if (it->second == c) + return it->first; + } + return std::string(); +} + + void Inset::dispatch(Cursor & cur, FuncRequest & cmd) { cur.updateFlags(Update::Force | Update::FitCursor); diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp index 55806f1781..21477ec151 100644 --- a/src/insets/InsetBibitem.cpp +++ b/src/insets/InsetBibitem.cpp @@ -59,7 +59,7 @@ void InsetBibitem::doDispatch(Cursor & cur, FuncRequest & cmd) switch (cmd.action) { case LFUN_INSET_MODIFY: { - InsetCommandParams p("bibitem"); + InsetCommandParams p(BIBITEM_CODE); InsetCommandMailer::string2params("bibitem", to_utf8(cmd.argument()), p); if (p.getCmdName().empty()) { cur.noUpdate(); diff --git a/src/insets/InsetBibtex.cpp b/src/insets/InsetBibtex.cpp index 8c308fcc6d..f2329ed035 100644 --- a/src/insets/InsetBibtex.cpp +++ b/src/insets/InsetBibtex.cpp @@ -89,7 +89,7 @@ void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd) switch (cmd.action) { case LFUN_INSET_MODIFY: { - InsetCommandParams p("bibtex"); + InsetCommandParams p(BIBTEX_CODE); try { if (!InsetCommandMailer::string2params("bibtex", to_utf8(cmd.argument()), p)) { diff --git a/src/insets/InsetCitation.h b/src/insets/InsetCitation.h index 31b8140a55..0bc7ee0ab7 100644 --- a/src/insets/InsetCitation.h +++ b/src/insets/InsetCitation.h @@ -15,6 +15,7 @@ #include "InsetCommand.h" +#include "InsetCode.h" #include "BiblioInfo.h" @@ -63,7 +64,7 @@ private: class Cache { public: /// - Cache() : engine(biblio::ENGINE_BASIC), params("citation") {} + Cache() : engine(biblio::ENGINE_BASIC), params(CITE_CODE) {} /// biblio::CiteEngine engine; /// diff --git a/src/insets/InsetCommand.cpp b/src/insets/InsetCommand.cpp index 477ebfcd42..c852c1c009 100644 --- a/src/insets/InsetCommand.cpp +++ b/src/insets/InsetCommand.cpp @@ -112,7 +112,7 @@ void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd) break; case LFUN_INSET_MODIFY: { - InsetCommandParams p(p_.insetType()); + InsetCommandParams p(p_.code()); InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p); if (p.getCmdName().empty()) cur.noUpdate(); @@ -187,6 +187,7 @@ string const InsetCommandMailer::inset2string(Buffer const &) const } +//FIXME This could take an InsetCode instead of a string bool InsetCommandMailer::string2params( string const & name, string const & in, InsetCommandParams & params) { @@ -219,6 +220,7 @@ bool InsetCommandMailer::string2params( } +//FIXME This could take an InsetCode instead of a string string const InsetCommandMailer::params2string(string const & name, InsetCommandParams const & params) diff --git a/src/insets/InsetCommandParams.cpp b/src/insets/InsetCommandParams.cpp index 3fd14f7a11..45201ff203 100644 --- a/src/insets/InsetCommandParams.cpp +++ b/src/insets/InsetCommandParams.cpp @@ -35,24 +35,21 @@ using support::ExceptionMessage; using support::WarningException; -//FIXME There is no reason now for this to take a string argument. -//It'd be much more robust if it took an InsetCode, since then -//the compiler would do some checking for us. -InsetCommandParams::InsetCommandParams(string const & insetType) - : insetType_(insetType), preview_(false) +InsetCommandParams::InsetCommandParams(InsetCode code) + : insetCode_(code), preview_(false) { - cmdName_ = getDefaultCmd(insetType); - info_ = findInfo(insetType, cmdName_); + cmdName_ = getDefaultCmd(code); + info_ = findInfo(code, cmdName_); BOOST_ASSERT(info_); params_.resize(info_->n); } -InsetCommandParams::InsetCommandParams(string const & insetType, +InsetCommandParams::InsetCommandParams(InsetCode code, string const & cmdName) - : insetType_(insetType), cmdName_(cmdName), preview_(false) + : insetCode_(code), cmdName_(cmdName), preview_(false) { - info_ = findInfo(insetType, cmdName); + info_ = findInfo(code, cmdName); BOOST_ASSERT(info_); params_.resize(info_->n); } @@ -64,30 +61,26 @@ InsetCommandParams::InsetCommandParams(string const & insetType, //a reason to use it in some cases? What should happen in the single //arg case, then? Maybe use the default? or leave that up to the inset? InsetCommandParams::CommandInfo const * -InsetCommandParams::findInfo(std::string const & insetType) + InsetCommandParams::findInfo(InsetCode code) { // No parameter may be named "preview", because that is a required // flag for all commands. - // InsetBibitem - if (insetType == "bibitem") { + switch (code) { + case BIBITEM_CODE: { static const char * const paramnames[] = {"label", "key", ""}; static const bool isoptional[] = {true, false}; static const CommandInfo info = {2, paramnames, isoptional}; return &info; } - - // InsetBibtex - if (insetType == "bibtex") { + case BIBTEX_CODE: { static const char * const paramnames[] = {"options", "btprint", "bibfiles", ""}; static const bool isoptional[] = {true, true, false}; static const CommandInfo info = {3, paramnames, isoptional}; return &info; } - - // InsetCitation - if (insetType == "citation") { + case CITE_CODE: { // standard cite does only take one argument if jurabib is // not used, but jurabib extends this to two arguments, so // we have to allow both here. InsetCitation takes care that @@ -98,127 +91,115 @@ InsetCommandParams::findInfo(std::string const & insetType) static const CommandInfo info = {3, paramnames, isoptional}; return &info; } - - // InsetFloatlist - if (insetType == "floatlist") { + case FLOAT_LIST_CODE: { static const char * const paramnames[] = {"type", ""}; static const bool isoptional[] = {false}; static const CommandInfo info = {1, paramnames, isoptional}; return &info; } - - // InsetHfill - if (insetType == "hfill") { + case HFILL_CODE: { static const char * const paramnames[] = {""}; static const CommandInfo info = {0, paramnames, 0}; return &info; } - - // InsetHyperlink - if (insetType == "href") { + case HYPERLINK_CODE: { static const char * const paramnames[] = {"name", "target", ""}; static const bool isoptional[] = {true, false}; static const CommandInfo info = {2, paramnames, isoptional}; return &info; } - - // InsetInclude - //FIXME This is really correct only for lstinputlistings, but it shouldn't - //cause a problem before we get things sorted out. Eventually, this calls - //InsetInclude::getParams(cmdName_), or something of the sort. - if (insetType == "include") { + case INCLUDE_CODE: { + //This is only correct for the case of listings, but it'll do for now. + //In the other cases, this second parameter should just be empty. static const char * const paramnames[] = {"filename", "lstparams", ""}; static const bool isoptional[] = {false, true}; static const CommandInfo info = {2, paramnames, isoptional}; return &info; } - - // InsetIndex, InsetPrintIndex, InsetLabel - if (insetType == "index" || insetType == "index_print" || insetType == "label") { + case INDEX_CODE: + case INDEX_PRINT_CODE: + case LABEL_CODE: { static const char * const paramnames[] = {"name", ""}; static const bool isoptional[] = {false}; static const CommandInfo info = {1, paramnames, isoptional}; return &info; } - - // InsetNomencl - if (insetType == "nomenclature") { + case NOMENCL_CODE: { static const char * const paramnames[] = {"prefix", "symbol", "description", ""}; static const bool isoptional[] = {true, false, false}; static const CommandInfo info = {3, paramnames, isoptional}; return &info; } - - // InsetPrintNomencl - if (insetType == "nomencl_print") { + case NOMENCL_PRINT_CODE: { static const char * const paramnames[] = {"labelwidth", ""}; static const bool isoptional[] = {true}; static const CommandInfo info = {1, paramnames, isoptional}; return &info; } - - // InsetRef - if (insetType == "ref") { + case REF_CODE: { static const char * const paramnames[] = {"name", "reference", ""}; static const bool isoptional[] = {true, false}; static const CommandInfo info = {2, paramnames, isoptional}; return &info; } - - // InsetTOC - if (insetType == "toc") { + case TOC_CODE: { static const char * const paramnames[] = {"type", ""}; static const bool isoptional[] = {false}; static const CommandInfo info = {1, paramnames, isoptional}; return &info; } - + default: + BOOST_ASSERT(false); + } return 0; } //FIXME Will eventually call a static method, etc. InsetCommandParams::CommandInfo const * - InsetCommandParams::findInfo(std::string const & insetType, - std::string const & cmdName) + InsetCommandParams::findInfo(InsetCode code, + std::string const &/* cmdName*/) { - return findInfo(insetType); + return findInfo(code); } //FIXME Should call InsetBibitem::getDefaultCmd(), eg -std::string InsetCommandParams::getDefaultCmd(std::string insetType) { - if (insetType == "bibitem") - return "bibitem"; - if (insetType == "bibtex") - return "bibtex"; //this is an unused dummy - if (insetType == "citation") - return "cite"; - if (insetType == "floatlist") - return "listoftables"; - if (insetType == "hfill") - return "hfill"; - if (insetType == "href") - return "href"; - if (insetType == "include") - return "include"; - if (insetType == "index") - return "index"; - if (insetType == "index_print") - return "print_index"; - if (insetType == "label") - return "label"; - if (insetType == "nomenclature") - return "nomenclature"; - if (insetType == "nomencl_print") - return "printnomenclature"; - if (insetType == "ref") - return "ref"; - if (insetType == "toc") - return "tableofcontents"; - BOOST_ASSERT(false); +std::string InsetCommandParams::getDefaultCmd(InsetCode code) { + switch (code) { + case BIBITEM_CODE: + return "bibitem"; + case BIBTEX_CODE: + return "bibtex"; //this is an unused dummy + case CITE_CODE: + return "cite"; + case FLOAT_LIST_CODE: + return "listoftables"; + case HFILL_CODE: + return "hfill"; + case HYPERLINK_CODE: + return "href"; + case INCLUDE_CODE: + return "include"; + case INDEX_CODE: + return "index"; + case INDEX_PRINT_CODE: + return "print_index"; + case LABEL_CODE: + return "label"; + case NOMENCL_CODE: + return "nomenclature"; + case NOMENCL_PRINT_CODE: + return "printnomenclature"; + case REF_CODE: + return "ref"; + case TOC_CODE: + return "tableofcontents"; + default: + BOOST_ASSERT(false); + } return ""; } @@ -227,9 +208,12 @@ void InsetCommandParams::setCmdName(string const & name) { //FIXME Check command compatibility cmdName_ = name; - BOOST_ASSERT(!insetType_.empty()); - CommandInfo const * const info = findInfo(insetType_, cmdName_); - BOOST_ASSERT(info); + CommandInfo const * const info = findInfo(insetCode_, cmdName_); + if (!info) { + lyxerr << "Command '" << name << "' is not compatible with a '" << + insetType() << "' inset." << std::endl; + return; + } ParamVector params(info->n); // Overtake parameters with the same name for (size_t i = 0; i < info_->n; ++i) { @@ -318,19 +302,18 @@ void InsetCommandParams::read(Lexer & lex) //FIXME if (lex.isOK()) { lex.next(); - string insetType = lex.getString(); - if (!insetType_.empty() && insetType != insetType_) { + string const insetType = lex.getString(); + InsetCode const code = insetCode(insetType); + if (code != insetCode_) { lex.printError("InsetCommand: Attempt to change type of parameters."); throw ExceptionMessage(WarningException, _("InsetCommand Error: "), from_utf8("Attempt to change type of parameters.")); } - // OK, we survived... - insetType_ = insetType; } if (lex.isOK()) { lex.next(); - string test = lex.getString(); + string const test = lex.getString(); if (test != "LatexCommand") { lex.printError("InsetCommand: no LatexCommand line found."); throw ExceptionMessage(WarningException, _("InsetCommand error:"), @@ -345,11 +328,11 @@ void InsetCommandParams::read(Lexer & lex) //upon what insetType_ is. //it's possible that should go into InsetCommand.cpp, //or maybe it's a standalone function. - info_ = findInfo(insetType_, cmdName_); + info_ = findInfo(insetCode_, cmdName_); if (!info_) { lex.printError("InsetCommand: Unknown inset name `$$Token'"); throw ExceptionMessage(WarningException, - _("Unknown inset name: "), from_utf8(insetType_)); + _("Unknown inset name: "), from_utf8(insetType())); } string token; @@ -387,7 +370,7 @@ void InsetCommandParams::read(Lexer & lex) void InsetCommandParams::write(ostream & os) const { - os << "CommandInset " << insetType_ << '\n'; + os << "CommandInset " << insetType() << '\n'; os << "LatexCommand " << cmdName_ << '\n'; for (size_t i = 0; i < info_->n; ++i) if (!params_[i].empty()) @@ -440,7 +423,7 @@ std::string const InsetCommandParams::getOptions() const if (info_->optional[i]) return to_utf8(params_[i]); lyxerr << "Programming error: get nonexisting option in " - << insetType_ << " inset." << endl; + << insetType() << " inset." << endl; return string(); } @@ -457,7 +440,7 @@ std::string const InsetCommandParams::getSecOptions() const } // Happens in InsetCitation lyxerr << "Programming error: get nonexisting second option in " - << insetType_ << " inset." << endl; + << insetType() << " inset." << endl; return string(); } @@ -480,7 +463,7 @@ void InsetCommandParams::setOptions(std::string const & o) return; } lyxerr << "Programming error: set nonexisting option in " - << insetType_ << " inset." << endl; + << insetType() << " inset." << endl; } @@ -498,7 +481,7 @@ void InsetCommandParams::setSecOptions(std::string const & s) } // Happens in InsetCitation lyxerr << "Programming error: set nonexisting second option in " - << insetType_ << " inset." << endl; + << insetType() << " inset." << endl; } @@ -539,7 +522,7 @@ void InsetCommandParams::clear() bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2) { - return o1.insetType_ == o2.insetType_ && + return o1.insetCode_ == o2.insetCode_ && o1.cmdName_ == o2.cmdName_ && o1.info_ == o2.info_ && o1.params_ == o2.params_ && diff --git a/src/insets/InsetCommandParams.h b/src/insets/InsetCommandParams.h index 00ff896570..a5dd8c462f 100644 --- a/src/insets/InsetCommandParams.h +++ b/src/insets/InsetCommandParams.h @@ -13,6 +13,7 @@ #ifndef INSETCOMMANDPARAMS_H #define INSETCOMMANDPARAMS_H +#include "InsetCode.h" #include "support/docstring.h" #include @@ -25,15 +26,16 @@ class Lexer; class InsetCommandParams { public: - /// Construct parameters for inset \p insetType, using - /// \p insetType as default for \p cmdName_. - explicit InsetCommandParams(std::string const & insetType); - /// Construct parameters for inset \p insetType with + /// Construct parameters for inset of type \p code. + explicit InsetCommandParams(InsetCode code); + /// Construct parameters for inset of type \p code with /// command name \p cmdName. - explicit InsetCommandParams(std::string const & insetType, + explicit InsetCommandParams(InsetCode code, std::string const & cmdName); /// - std::string insetType() const { return insetType_; } + std::string insetType() const { return insetName(insetCode_); } + /// + InsetCode code() const { return insetCode_; } /// void read(Lexer &); /// Parse the command @@ -82,21 +84,21 @@ private: /// Tells whether a parameter is optional bool const * optional; }; - /// Get information for inset type \p insetType. + /// Get information for inset type \p code. /// Returns 0 if the inset is not known. - static CommandInfo const * findInfo(std::string const & insetType); - /// Get information for \p insetType and command \p cmdName. + static CommandInfo const * findInfo(InsetCode code); + /// Get information for \p code and command \p cmdName. /// Returns 0 if the combination is not known. /// Don't call this without first making sure the command name is /// acceptable to the inset. - static CommandInfo const * findInfo(std::string const & insetType, + static CommandInfo const * findInfo(InsetCode code, std::string const & cmdName); /// - std::string getDefaultCmd(std::string insetType); + std::string getDefaultCmd(InsetCode); /// Description of all command properties CommandInfo const * info_; /// what kind of inset we're the parameters for - std::string insetType_; + InsetCode insetCode_; /// The name of this command as it appears in .lyx and .tex files std::string cmdName_; /// diff --git a/src/insets/InsetFloatList.cpp b/src/insets/InsetFloatList.cpp index 408e6626e3..808bbd4f6c 100644 --- a/src/insets/InsetFloatList.cpp +++ b/src/insets/InsetFloatList.cpp @@ -38,12 +38,12 @@ using std::ostream; InsetFloatList::InsetFloatList() - : InsetCommand(InsetCommandParams("floatlist"), "toc") + : InsetCommand(InsetCommandParams(FLOAT_LIST_CODE), "toc") {} InsetFloatList::InsetFloatList(string const & type) - : InsetCommand(InsetCommandParams("floatlist"), "toc") + : InsetCommand(InsetCommandParams(FLOAT_LIST_CODE), "toc") { setParam("type", from_ascii(type)); } diff --git a/src/insets/InsetHFill.cpp b/src/insets/InsetHFill.cpp index 476ffdb79b..39d901dcac 100644 --- a/src/insets/InsetHFill.cpp +++ b/src/insets/InsetHFill.cpp @@ -20,7 +20,7 @@ namespace lyx { InsetHFill::InsetHFill() - : InsetCommand(InsetCommandParams("hfill"), std::string()) + : InsetCommand(InsetCommandParams(HFILL_CODE), std::string()) {} diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index 38fd582689..cfd0e6ea78 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -136,7 +136,7 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd) switch (cmd.action) { case LFUN_INSET_MODIFY: { - InsetCommandParams p("include"); + InsetCommandParams p(INCLUDE_CODE); InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p); if (!p.getCmdName().empty()) { if (isListings(p)){ diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index 3d8ffa4b97..3122e95947 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -54,7 +54,7 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) switch (cmd.action) { case LFUN_INSET_MODIFY: { - InsetCommandParams p("label"); + InsetCommandParams p(LABEL_CODE); // FIXME UNICODE InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p); if (p.getCmdName().empty()) { diff --git a/src/insets/InsetRef.cpp b/src/insets/InsetRef.cpp index 49606c425b..f1838c28fc 100644 --- a/src/insets/InsetRef.cpp +++ b/src/insets/InsetRef.cpp @@ -84,7 +84,7 @@ int InsetRef::latex(Buffer const &, odocstream & os, { // We don't want to output p_["name"], since that is only used // in docbook. So we construct new params, without it, and use that. - InsetCommandParams p("ref", getCmdName()); + InsetCommandParams p(REF_CODE, getCmdName()); p["reference"] = getParam("reference"); os << escape(p.getCommand()); return 0; diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index 3c19dc6c05..d4ba42f721 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -1095,7 +1095,7 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd) if (old_label.empty()) old_label = default_label; - InsetCommandParams p("label"); + InsetCommandParams p(LABEL_CODE); p["name"] = cmd.argument().empty() ? old_label : cmd.argument(); std::string const data = InsetCommandMailer::params2string("label", p); @@ -1112,7 +1112,7 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd) //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl; std::string const name = cmd.getArg(0); if (name == "label") { - InsetCommandParams p("label"); + InsetCommandParams p(LABEL_CODE); InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p); docstring str = p["name"]; cur.recordUndoInset(); diff --git a/src/mathed/InsetMathRef.cpp b/src/mathed/InsetMathRef.cpp index 64c474f12e..48b74a8aa8 100644 --- a/src/mathed/InsetMathRef.cpp +++ b/src/mathed/InsetMathRef.cpp @@ -174,7 +174,7 @@ int InsetMathRef::docbook(Buffer const & buf, odocstream & os, string const InsetMathRef::createDialogStr(string const & name) const { - InsetCommandParams icp(to_ascii(commandname())); + InsetCommandParams icp(REF_CODE, to_ascii(commandname())); icp["reference"] = asString(cell(0)); if (!cell(1).empty()) icp["name"] = asString(cell(1)); diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 00e90f7785..2127f65eb8 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -424,7 +424,7 @@ bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar) if (name != "ref" ) return false; - InsetCommandParams icp("ref"); + InsetCommandParams icp(REF_CODE); // FIXME UNICODE InsetCommandMailer::string2params("ref", to_utf8(str), icp); mathed_parse_cell(ar, icp.getCommand()); -- 2.39.2