]> git.lyx.org Git - lyx.git/blob - src/CmdDef.cpp
f7dbb71a0921a167a0efeaa488f6240505b44f5c
[lyx.git] / src / CmdDef.cpp
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.
5  *
6  * \author Bernhard Roider
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "CmdDef.h"
14
15 #include "LyXAction.h"
16 #include "Lexer.h"
17
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21
22 #include <ostream>
23 #include <string>
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace lyx {
29
30 namespace {
31
32 enum CmdDefTags {
33         BN_DEFFILE,
34         BN_DEFINE
35 };
36
37 keyword_item cmdDefTags[] = {
38         { "\\def_file", BN_DEFFILE },
39         { "\\define", BN_DEFINE }
40 };
41
42 }
43
44
45 bool CmdDef::read(string const & def_file)
46 {
47         const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
48
49         Lexer lexrc(cmdDefTags, cmdDefCount);
50         if (lyxerr.debugging(Debug::PARSER))
51                 lexrc.printTable(lyxerr);
52
53         FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
54         lexrc.setFile(tmp);
55         if (!lexrc.isOK()) {
56                 lyxerr << "CmdDef::read: cannot open def file:"
57                        << tmp << endl;
58                 return false;
59         }
60
61         bool error = false;
62         while (lexrc.isOK()) {
63                 switch (lexrc.lex()) {
64                 case Lexer::LEX_UNDEF:
65                         lexrc.printError("Unknown tag `$$Token'");
66                         error = true;
67                         continue;
68                 case Lexer::LEX_FEOF:
69                         continue;
70                 case BN_DEFINE:
71                 {
72                         string name, def;
73
74                         if (lexrc.next()) {
75                                 name = lexrc.getString();
76                         } else {
77                                 lexrc.printError("BN_DEFINE: Missing command name");
78                                 error = true;
79                                 break;
80                         }
81
82                         if (lexrc.next(true)) {
83                                 def = lexrc.getString();
84                         } else {
85                                 lexrc.printError("BN_DEFINE: missing command definition");
86                                 error = true;
87                                 break;
88                         }
89
90                         newCmdDefResult e = newCmdDef(name, def);
91                         switch (e) {
92                                 case CmdDefNameEmpty:
93                                         lexrc.printError("BN_DEFINE: Command name is empty");
94                                         error = true;
95                                         break;
96                                 case CmdDefExists:
97                                         lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
98                                         error = true;
99                                         break;
100                                 case CmdDefInvalid:
101                                         lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
102                                         error = true;
103                         }
104
105                         break;
106                 }
107                 case BN_DEFFILE:
108                         if (lexrc.next()) {
109                                 string const tmp(lexrc.getString());
110                                 error |= !read(tmp);
111                         } else {
112                                 lexrc.printError("BN_DEFFILE: Missing file name");
113                                 error = true;
114                                 break;
115
116                         }
117                         break;
118                 }
119         }
120
121         if (error)
122                 lyxerr << "CmdDef::read: error while reading def file:"
123                        << tmp << endl;
124         return !error;
125 }
126
127
128 bool CmdDef::lock(string const & name, FuncRequest & func)
129 {
130         if (cmdDefMap.empty()) {
131                 func = FuncRequest::unknown;
132                 return false;
133         }
134
135         string const name2 = trim(name);
136
137         if (lockSet.find(name2) != lockSet.end()) {
138                 func = FuncRequest::noaction;
139                 return false;
140         }
141
142         CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
143
144         if (pos == cmdDefMap.end()) {
145                 func = FuncRequest::unknown;
146                 return false;
147         }
148
149         lockSet.insert(name2);
150         func = pos->second;
151         return true;
152 }
153
154
155 void CmdDef::release(string const & name)
156 {
157         string const name2 = trim(name);
158
159         lockSet.erase(name2);
160 }
161
162 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name, 
163                                                                                   string const & def)
164 {
165         string const name2 = trim(name);
166
167         if (name2.empty()) 
168                 return CmdDefNameEmpty;
169
170         if (cmdDefMap.find(name) != cmdDefMap.end())
171                 return CmdDefExists;
172
173         FuncRequest     func = lyxaction.lookupFunc(def);
174         if (func.action == LFUN_NOACTION ||
175                 func.action == LFUN_UNKNOWN_ACTION) {
176                         return CmdDefInvalid;
177         }
178
179         cmdDefMap[name2] = func;
180
181         return CmdDefOk;
182 }
183
184
185 } // namespace lyx