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/FileName.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
27 using namespace lyx::support;
38 keyword_item cmdDefTags[] = {
39 { "\\def_file", BN_DEFFILE },
40 { "\\define", BN_DEFINE }
46 bool CmdDef::read(string const & def_file)
48 const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
50 Lexer lexrc(cmdDefTags, cmdDefCount);
51 if (lyxerr.debugging(Debug::PARSER))
52 lexrc.printTable(lyxerr);
54 FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
57 lyxerr << "CmdDef::read: cannot open def file:"
63 while (lexrc.isOK()) {
64 switch (lexrc.lex()) {
65 case Lexer::LEX_UNDEF:
66 lexrc.printError("Unknown tag `$$Token'");
76 name = lexrc.getString();
78 lexrc.printError("BN_DEFINE: Missing command name");
83 if (lexrc.next(true)) {
84 def = lexrc.getString();
86 lexrc.printError("BN_DEFINE: missing command definition");
91 newCmdDefResult e = newCmdDef(name, def);
94 lexrc.printError("BN_DEFINE: Command name is empty");
98 lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
102 lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
110 string const tmp(lexrc.getString());
113 lexrc.printError("BN_DEFFILE: Missing file name");
123 lyxerr << "CmdDef::read: error while reading def file:"
129 bool CmdDef::lock(string const & name, FuncRequest & func)
131 if (cmdDefMap.empty()) {
132 func = FuncRequest::unknown;
136 string const name2 = trim(name);
138 if (lockSet.find(name2) != lockSet.end()) {
139 func = FuncRequest::noaction;
143 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
145 if (pos == cmdDefMap.end()) {
146 func = FuncRequest::unknown;
150 lockSet.insert(name2);
156 void CmdDef::release(string const & name)
158 string const name2 = trim(name);
160 lockSet.erase(name2);
163 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name,
166 string const name2 = trim(name);
169 return CmdDefNameEmpty;
171 if (cmdDefMap.find(name) != cmdDefMap.end())
174 FuncRequest func = lyxaction.lookupFunc(def);
175 if (func.action == LFUN_NOACTION ||
176 func.action == LFUN_UNKNOWN_ACTION) {
177 return CmdDefInvalid;
180 cmdDefMap[name2] = func;