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"
29 using support::FileName;
30 using support::i18nLibFileSearch;
41 keyword_item cmdDefTags[] = {
42 { "\\def_file", BN_DEFFILE },
43 { "\\define", BN_DEFINE }
49 bool CmdDef::read(string const & def_file)
51 const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
53 Lexer lexrc(cmdDefTags, cmdDefCount);
54 if (lyxerr.debugging(Debug::PARSER))
55 lexrc.printTable(lyxerr);
57 FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
60 lyxerr << "CmdDef::read: cannot open def file:"
65 //LYXERR(Debug::KBMAP, "Reading def file:" << tmp);
68 while (lexrc.isOK()) {
69 switch (lexrc.lex()) {
70 case Lexer::LEX_UNDEF:
71 lexrc.printError("Unknown tag `$$Token'");
81 name = lexrc.getString();
83 lexrc.printError("BN_DEFINE: Missing command name");
88 if (lexrc.next(true)) {
89 def = lexrc.getString();
91 lexrc.printError("BN_DEFINE: missing command definition");
96 newCmdDefResult e = newCmdDef(name, def);
99 lexrc.printError("BN_DEFINE: Command name is empty");
103 lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
107 lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
115 string const tmp(lexrc.getString());
118 lexrc.printError("BN_DEFFILE: Missing file name");
128 lyxerr << "CmdDef::read: error while reading def file:"
134 bool CmdDef::lock(string const & name, FuncRequest & func)
136 if (cmdDefMap.empty())
138 func = FuncRequest::unknown;
142 string const name2 = trim(name);
144 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
146 if (pos == cmdDefMap.end())
148 func = FuncRequest::unknown;
152 if (pos->second->locked)
154 func = FuncRequest::noaction;
158 pos->second->locked = true;
159 func = pos->second->func;
164 void CmdDef::release(string const & name)
166 if (cmdDefMap.empty())
169 string const name2 = trim(name);
171 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
173 if (pos == cmdDefMap.end())
176 pos->second->locked = false;
179 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name,
182 string const name2 = trim(name);
185 return CmdDefNameEmpty;
187 if (cmdDefMap.find(name) != cmdDefMap.end())
190 FuncRequest func = lyxaction.lookupFunc(def);
191 if (func.action == LFUN_NOACTION ||
192 func.action == LFUN_UNKNOWN_ACTION) {
193 return CmdDefInvalid;
196 boost::shared_ptr<CmdDefInfo> info;
197 info.reset(new CmdDefInfo(func));
198 cmdDefMap[name2] = info;