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