3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Bernhard Roider
8 * Full author contact details are available in file CREDITS.
15 #include "LyXAction.h"
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
26 using namespace lyx::support;
37 keyword_item cmdDefTags[] = {
38 { "\\def_file", BN_DEFFILE },
39 { "\\define", BN_DEFINE }
45 bool CmdDef::read(string const & def_file)
47 const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
49 Lexer lexrc(cmdDefTags, cmdDefCount);
50 if (lyxerr.debugging(Debug::PARSER))
51 lexrc.printTable(lyxerr);
53 FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
56 lyxerr << "CmdDef::read: cannot open def file:"
62 while (lexrc.isOK()) {
63 switch (lexrc.lex()) {
64 case Lexer::LEX_UNDEF:
65 lexrc.printError("Unknown tag `$$Token'");
75 name = lexrc.getString();
77 lexrc.printError("BN_DEFINE: Missing command name");
82 if (lexrc.next(true)) {
83 def = lexrc.getString();
85 lexrc.printError("BN_DEFINE: missing command definition");
90 newCmdDefResult e = newCmdDef(name, def);
93 lexrc.printError("BN_DEFINE: Command name is empty");
97 lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
101 lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
109 string const tmp(lexrc.getString());
112 lexrc.printError("BN_DEFFILE: Missing file name");
122 lyxerr << "CmdDef::read: error while reading def file:"
128 bool CmdDef::lock(string const & name, FuncRequest & func)
130 if (cmdDefMap.empty()) {
131 func = FuncRequest::unknown;
135 string const name2 = trim(name);
137 if (lockSet.find(name2) != lockSet.end()) {
138 func = FuncRequest::noaction;
142 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
144 if (pos == cmdDefMap.end()) {
145 func = FuncRequest::unknown;
149 lockSet.insert(name2);
155 void CmdDef::release(string const & name)
157 string const name2 = trim(name);
159 lockSet.erase(name2);
162 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name,
165 string const name2 = trim(name);
168 return CmdDefNameEmpty;
170 if (cmdDefMap.find(name) != cmdDefMap.end())
173 FuncRequest func = lyxaction.lookupFunc(def);
174 if (func.action == LFUN_NOACTION ||
175 func.action == LFUN_UNKNOWN_ACTION) {
176 return CmdDefInvalid;
179 cmdDefMap[name2] = func;