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"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
27 using support::FileName;
28 using support::i18nLibFileSearch;
39 keyword_item cmdDefTags[] = {
40 { "\\def_file", BN_DEFFILE },
41 { "\\define", BN_DEFINE }
47 bool CmdDef::read(string const & def_file)
49 const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
51 Lexer lexrc(cmdDefTags, cmdDefCount);
52 if (lyxerr.debugging(Debug::PARSER))
53 lexrc.printTable(lyxerr);
55 FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
58 lyxerr << "CmdDef::read: cannot open def file:"
63 // LYXERR(Debug::KBMAP) << "Reading def file:" << tmp << endl;
66 while (lexrc.isOK()) {
67 switch (lexrc.lex()) {
68 case Lexer::LEX_UNDEF:
69 lexrc.printError("Unknown tag `$$Token'");
79 name = lexrc.getString();
81 lexrc.printError("BN_DEFINE: Missing command name");
86 if (lexrc.next(true)) {
87 def = lexrc.getString();
89 lexrc.printError("BN_DEFINE: missing command definition");
94 newCmdDefResult e = newCmdDef(name, def);
97 lexrc.printError("BN_DEFINE: Command name is empty");
101 lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
105 lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
113 string const tmp(lexrc.getString());
116 lexrc.printError("BN_DEFFILE: Missing file name");
126 lyxerr << "CmdDef::read: error while reading def file:"
132 bool CmdDef::lock(string const & name, FuncRequest & func)
134 if (cmdDefMap.empty())
136 func = FuncRequest::unknown;
140 string const name2 = trim(name);
142 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
144 if (pos == cmdDefMap.end())
146 func = FuncRequest::unknown;
150 if (pos->second->locked)
152 func = FuncRequest::noaction;
156 pos->second->locked = true;
157 func = pos->second->func;
162 void CmdDef::release(string const & name)
164 if (cmdDefMap.empty())
167 string const name2 = trim(name);
169 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
171 if (pos == cmdDefMap.end())
174 pos->second->locked = false;
177 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name,
180 string const name2 = trim(name);
183 return CmdDefNameEmpty;
185 if (cmdDefMap.find(name) != cmdDefMap.end())
188 FuncRequest func = lyxaction.lookupFunc(def);
189 if (func.action == LFUN_NOACTION ||
190 func.action == LFUN_UNKNOWN_ACTION) {
191 return CmdDefInvalid;
194 boost::shared_ptr<CmdDefInfo> info;
195 info.reset(new CmdDefInfo(func));
196 cmdDefMap[name2] = info;