X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FCmdDef.cpp;h=779ac8270a36dbed39b26dbe518431e493cc66d4;hb=28be7d552f62cc02fa86d7f79201d089bfb2d7b5;hp=2508fab8658671c20b0d74c500c1e39d2c0fb142;hpb=1a77c867a23f6f904f24b4968bdeb41e4244ccec;p=lyx.git diff --git a/src/CmdDef.cpp b/src/CmdDef.cpp index 2508fab865..779ac8270a 100644 --- a/src/CmdDef.cpp +++ b/src/CmdDef.cpp @@ -13,60 +13,46 @@ #include "CmdDef.h" #include "LyXAction.h" -#include "debug.h" #include "Lexer.h" +#include "support/debug.h" +#include "support/FileName.h" #include "support/filetools.h" #include "support/lstrings.h" -using std::endl; -using std::string; +#include -namespace lyx { - -using support::FileName; -using support::i18nLibFileSearch; -using support::trim; - - -namespace { - -enum CmdDefTags { - BN_DEFFILE, - BN_DEFINE -}; - -keyword_item cmdDefTags[] = { - { "\\def_file", BN_DEFFILE }, - { "\\define", BN_DEFINE } -}; - -} +using namespace std; +using namespace lyx::support; +namespace lyx { bool CmdDef::read(string const & def_file) { - const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item); + enum { + BN_DEFFILE, + BN_DEFINE + }; - Lexer lexrc(cmdDefTags, cmdDefCount); - if (lyxerr.debugging(Debug::PARSER)) - lexrc.printTable(lyxerr); + LexerKeyword cmdDefTags[] = { + { "\\def_file", BN_DEFFILE }, + { "\\define", BN_DEFINE } + }; + Lexer lex(cmdDefTags); FileName const tmp(i18nLibFileSearch("commands", def_file, "def")); - lexrc.setFile(tmp); - if (!lexrc.isOK()) { - lyxerr << "CmdDef::read: cannot open def file:" - << tmp << endl; + lex.setContext("CmdDef::read"); + lex.setFile(tmp); + if (!lex.isOK()) { + LYXERR0( "CmdDef::read: cannot open def file:" << tmp); return false; } -// LYXERR(Debug::KBMAP) << "Reading def file:" << tmp << endl; - bool error = false; - while (lexrc.isOK()) { - switch (lexrc.lex()) { + while (lex.isOK()) { + switch (lex.lex()) { case Lexer::LEX_UNDEF: - lexrc.printError("Unknown tag `$$Token'"); + lex.printError("Unknown tag"); error = true; continue; case Lexer::LEX_FEOF: @@ -75,45 +61,48 @@ bool CmdDef::read(string const & def_file) { string name, def; - if (lexrc.next()) { - name = lexrc.getString(); + if (lex.next()) { + name = lex.getString(); } else { - lexrc.printError("BN_DEFINE: Missing command name"); + lex.printError("BN_DEFINE: Missing command name"); error = true; break; } - if (lexrc.next(true)) { - def = lexrc.getString(); + if (lex.next(true)) { + def = lex.getString(); } else { - lexrc.printError("BN_DEFINE: missing command definition"); + lex.printError("BN_DEFINE: missing command definition"); error = true; break; } newCmdDefResult e = newCmdDef(name, def); switch (e) { - case CmdDefNameEmpty: - lexrc.printError("BN_DEFINE: Command name is empty"); - error = true; - break; - case CmdDefExists: - lexrc.printError("BN_DEFINE: Command `" + name + "' already defined"); - error = true; - break; - case CmdDefInvalid: - lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid"); - error = true; + case CmdDefNameEmpty: + lex.printError("BN_DEFINE: Command name is empty"); + error = true; + break; + case CmdDefExists: + lex.printError("BN_DEFINE: Command `" + name + "' already defined"); + error = true; + break; + case CmdDefInvalid: + lex.printError("BN_DEFINE: Command definition for `" + name + "' is not valid"); + error = true; + break; + case CmdDefOk: + break; } break; } case BN_DEFFILE: - if (lexrc.next()) { - string const tmp(lexrc.getString()); + if (lex.next()) { + string const tmp = lex.getString(); error |= !read(tmp); } else { - lexrc.printError("BN_DEFFILE: Missing file name"); + lex.printError("BN_DEFFILE: Missing file name"); error = true; break; @@ -123,59 +112,47 @@ bool CmdDef::read(string const & def_file) } if (error) - lyxerr << "CmdDef::read: error while reading def file:" - << tmp << endl; + LYXERR0("CmdDef::read: error while reading def file:" << tmp); return !error; } bool CmdDef::lock(string const & name, FuncRequest & func) { - if (cmdDefMap.empty()) - { + if (cmdDefMap.empty()) { func = FuncRequest::unknown; return false; } string const name2 = trim(name); - CmdDefMap::const_iterator pos = cmdDefMap.find(name2); - - if (pos == cmdDefMap.end()) - { - func = FuncRequest::unknown; + if (lockSet.find(name2) != lockSet.end()) { + func = FuncRequest::noaction; return false; } - if (pos->second->locked) - { - func = FuncRequest::noaction; + CmdDefMap::const_iterator pos = cmdDefMap.find(name2); + + if (pos == cmdDefMap.end()) { + func = FuncRequest::unknown; return false; } - pos->second->locked = true; - func = pos->second->func; + lockSet.insert(name2); + func = pos->second; return true; } void CmdDef::release(string const & name) { - if (cmdDefMap.empty()) - return; - string const name2 = trim(name); - - CmdDefMap::const_iterator pos = cmdDefMap.find(name2); - - if (pos == cmdDefMap.end()) - return; - - pos->second->locked = false; + lockSet.erase(name2); } + CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name, - string const & def) + string const & def) { string const name2 = trim(name); @@ -186,14 +163,12 @@ CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name, return CmdDefExists; FuncRequest func = lyxaction.lookupFunc(def); - if (func.action == LFUN_NOACTION || - func.action == LFUN_UNKNOWN_ACTION) { + if (func.action() == LFUN_NOACTION + || func.action() == LFUN_UNKNOWN_ACTION) { return CmdDefInvalid; } - boost::shared_ptr info; - info.reset(new CmdDefInfo(func)); - cmdDefMap[name2] = info; + cmdDefMap[name2] = func; return CmdDefOk; }