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