]> git.lyx.org Git - features.git/blob - src/CmdDef.cpp
* get rid of support::absolutePath()
[features.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                         }
105
106                         break;
107                 }
108                 case BN_DEFFILE:
109                         if (lexrc.next()) {
110                                 string const tmp(lexrc.getString());
111                                 error |= !read(tmp);
112                         } else {
113                                 lexrc.printError("BN_DEFFILE: Missing file name");
114                                 error = true;
115                                 break;
116
117                         }
118                         break;
119                 }
120         }
121
122         if (error)
123                 lyxerr << "CmdDef::read: error while reading def file:"
124                        << tmp << endl;
125         return !error;
126 }
127
128
129 bool CmdDef::lock(string const & name, FuncRequest & func)
130 {
131         if (cmdDefMap.empty()) {
132                 func = FuncRequest::unknown;
133                 return false;
134         }
135
136         string const name2 = trim(name);
137
138         if (lockSet.find(name2) != lockSet.end()) {
139                 func = FuncRequest::noaction;
140                 return false;
141         }
142
143         CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
144
145         if (pos == cmdDefMap.end()) {
146                 func = FuncRequest::unknown;
147                 return false;
148         }
149
150         lockSet.insert(name2);
151         func = pos->second;
152         return true;
153 }
154
155
156 void CmdDef::release(string const & name)
157 {
158         string const name2 = trim(name);
159
160         lockSet.erase(name2);
161 }
162
163 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name, 
164                                                                                   string const & def)
165 {
166         string const name2 = trim(name);
167
168         if (name2.empty()) 
169                 return CmdDefNameEmpty;
170
171         if (cmdDefMap.find(name) != cmdDefMap.end())
172                 return CmdDefExists;
173
174         FuncRequest     func = lyxaction.lookupFunc(def);
175         if (func.action == LFUN_NOACTION ||
176                 func.action == LFUN_UNKNOWN_ACTION) {
177                         return CmdDefInvalid;
178         }
179
180         cmdDefMap[name2] = func;
181
182         return CmdDefOk;
183 }
184
185
186 } // namespace lyx