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