* qt_helpers.cpp:
[lyx.git] / src / CmdDef.cpp
blobb0b03350c7fbf1307c4aa5818d4c03df35dc707f
1 /**
2 * \file CmdDef.cpp
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.
9 */
11 #include <config.h>
13 #include "CmdDef.h"
15 #include "LyXAction.h"
16 #include "Lexer.h"
18 #include "support/debug.h"
19 #include "support/FileName.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
23 #include <string>
25 using namespace std;
26 using namespace lyx::support;
28 namespace lyx {
30 bool CmdDef::read(string const & def_file)
32 enum {
33 BN_DEFFILE,
34 BN_DEFINE
37 LexerKeyword cmdDefTags[] = {
38 { "\\def_file", BN_DEFFILE },
39 { "\\define", BN_DEFINE }
42 Lexer lex(cmdDefTags);
43 FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
44 lex.setContext("CmdDef::read");
45 lex.setFile(tmp);
46 if (!lex.isOK()) {
47 LYXERR0( "CmdDef::read: cannot open def file:" << tmp);
48 return false;
51 bool error = false;
52 while (lex.isOK()) {
53 switch (lex.lex()) {
54 case Lexer::LEX_UNDEF:
55 lex.printError("Unknown tag");
56 error = true;
57 continue;
58 case Lexer::LEX_FEOF:
59 continue;
60 case BN_DEFINE:
62 string name, def;
64 if (lex.next()) {
65 name = lex.getString();
66 } else {
67 lex.printError("BN_DEFINE: Missing command name");
68 error = true;
69 break;
72 if (lex.next(true)) {
73 def = lex.getString();
74 } else {
75 lex.printError("BN_DEFINE: missing command definition");
76 error = true;
77 break;
80 newCmdDefResult e = newCmdDef(name, def);
81 switch (e) {
82 case CmdDefNameEmpty:
83 lex.printError("BN_DEFINE: Command name is empty");
84 error = true;
85 break;
86 case CmdDefExists:
87 lex.printError("BN_DEFINE: Command `" + name + "' already defined");
88 error = true;
89 break;
90 case CmdDefInvalid:
91 lex.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
92 error = true;
93 break;
94 case CmdDefOk:
95 break;
98 break;
100 case BN_DEFFILE:
101 if (lex.next()) {
102 string const tmp = lex.getString();
103 error |= !read(tmp);
104 } else {
105 lex.printError("BN_DEFFILE: Missing file name");
106 error = true;
107 break;
110 break;
114 if (error)
115 LYXERR0("CmdDef::read: error while reading def file:" << tmp);
116 return !error;
120 bool CmdDef::lock(string const & name, FuncRequest & func)
122 if (cmdDefMap.empty()) {
123 func = FuncRequest::unknown;
124 return false;
127 string const name2 = trim(name);
129 if (lockSet.find(name2) != lockSet.end()) {
130 func = FuncRequest::noaction;
131 return false;
134 CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
136 if (pos == cmdDefMap.end()) {
137 func = FuncRequest::unknown;
138 return false;
141 lockSet.insert(name2);
142 func = pos->second;
143 return true;
147 void CmdDef::release(string const & name)
149 string const name2 = trim(name);
150 lockSet.erase(name2);
154 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name,
155 string const & def)
157 string const name2 = trim(name);
159 if (name2.empty())
160 return CmdDefNameEmpty;
162 if (cmdDefMap.find(name) != cmdDefMap.end())
163 return CmdDefExists;
165 FuncRequest func = lyxaction.lookupFunc(def);
166 if (func.action == LFUN_NOACTION
167 || func.action == LFUN_UNKNOWN_ACTION) {
168 return CmdDefInvalid;
171 cmdDefMap[name2] = func;
173 return CmdDefOk;
177 } // namespace lyx