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");
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()) {
135 func
= FuncRequest::unknown
;
139 string
const name2
= trim(name
);
141 if (lockSet
.find(name2
) != lockSet
.end()) {
142 func
= FuncRequest::noaction
;
146 CmdDefMap::const_iterator pos
= cmdDefMap
.find(name2
);
148 if (pos
== cmdDefMap
.end()) {
149 func
= FuncRequest::unknown
;
153 lockSet
.insert(name2
);
159 void CmdDef::release(string
const & name
)
161 string
const name2
= trim(name
);
163 lockSet
.erase(name2
);
166 CmdDef::newCmdDefResult
CmdDef::newCmdDef(string
const & name
,
169 string
const name2
= trim(name
);
172 return CmdDefNameEmpty
;
174 if (cmdDefMap
.find(name
) != cmdDefMap
.end())
177 FuncRequest func
= lyxaction
.lookupFunc(def
);
178 if (func
.action
== LFUN_NOACTION
||
179 func
.action
== LFUN_UNKNOWN_ACTION
) {
180 return CmdDefInvalid
;
183 cmdDefMap
[name2
] = func
;