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