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"
30 using support::FileName;
31 using support::i18nLibFileSearch;
42 keyword_item cmdDefTags[] = {
43 { "\\def_file", BN_DEFFILE },
44 { "\\define", BN_DEFINE }
50 bool CmdDef::read(string const & def_file)
52 const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
54 Lexer lexrc(cmdDefTags, cmdDefCount);
55 if (lyxerr.debugging(Debug::PARSER))
56 lexrc.printTable(lyxerr);
58 FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
61 lyxerr << "CmdDef::read: cannot open def file:"
67 while (lexrc.isOK()) {
68 switch (lexrc.lex()) {
69 case Lexer::LEX_UNDEF:
70 lexrc.printError("Unknown tag `$$Token'");
80 name = lexrc.getString();
82 lexrc.printError("BN_DEFINE: Missing command name");
87 if (lexrc.next(true)) {
88 def = lexrc.getString();
90 lexrc.printError("BN_DEFINE: missing command definition");
95 newCmdDefResult e = newCmdDef(name, def);
98 lexrc.printError("BN_DEFINE: Command name is empty");
102 lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
106 lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
114 string const tmp(lexrc.getString());
117 lexrc.printError("BN_DEFFILE: Missing file name");
127 lyxerr << "CmdDef::read: error while reading def file:"
133 bool CmdDef::lock(string const & name, FuncRequest & func)
135 if (cmdDefMap.empty()) {
136 func = FuncRequest::unknown;
140 string const name2 = trim(name);
142 if (lockSet.find(name2) != lockSet.end()) {
143 func = FuncRequest::noaction;
147 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
149 if (pos == cmdDefMap.end()) {
150 func = FuncRequest::unknown;
154 lockSet.insert(name2);
160 void CmdDef::release(string const & name)
162 string const name2 = trim(name);
164 lockSet.erase(name2);
167 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name,
170 string const name2 = trim(name);
173 return CmdDefNameEmpty;
175 if (cmdDefMap.find(name) != cmdDefMap.end())
178 FuncRequest func = lyxaction.lookupFunc(def);
179 if (func.action == LFUN_NOACTION ||
180 func.action == LFUN_UNKNOWN_ACTION) {
181 return CmdDefInvalid;
184 cmdDefMap[name2] = func;