]> git.lyx.org Git - lyx.git/blob - src/CmdDef.cpp
Fix dialog handling of Insert Plain Text
[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 "debug.h"
17 #include "Lexer.h"
18
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21
22 using std::endl;
23 using std::string;
24
25 namespace lyx {
26
27 using support::FileName;
28 using support::i18nLibFileSearch;
29 using support::trim;
30
31
32 namespace {
33
34 enum CmdDefTags {
35         BN_DEFFILE,
36         BN_DEFINE
37 };
38
39 keyword_item cmdDefTags[] = {
40         { "\\def_file", BN_DEFFILE },
41         { "\\define", BN_DEFINE }
42 };
43
44 }
45
46
47 bool CmdDef::read(string const & def_file)
48 {
49         const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
50
51         Lexer lexrc(cmdDefTags, cmdDefCount);
52         if (lyxerr.debugging(Debug::PARSER))
53                 lexrc.printTable(lyxerr);
54
55         FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
56         lexrc.setFile(tmp);
57         if (!lexrc.isOK()) {
58                 lyxerr << "CmdDef::read: cannot open def file:"
59                        << tmp << endl;
60                 return false;
61         }
62
63         //LYXERR(Debug::KBMAP, "Reading def file:" << tmp);
64
65         bool error = false;
66         while (lexrc.isOK()) {
67                 switch (lexrc.lex()) {
68                 case Lexer::LEX_UNDEF:
69                         lexrc.printError("Unknown tag `$$Token'");
70                         error = true;
71                         continue;
72                 case Lexer::LEX_FEOF:
73                         continue;
74                 case BN_DEFINE:
75                 {
76                         string name, def;
77
78                         if (lexrc.next()) {
79                                 name = lexrc.getString();
80                         } else {
81                                 lexrc.printError("BN_DEFINE: Missing command name");
82                                 error = true;
83                                 break;
84                         }
85
86                         if (lexrc.next(true)) {
87                                 def = lexrc.getString();
88                         } else {
89                                 lexrc.printError("BN_DEFINE: missing command definition");
90                                 error = true;
91                                 break;
92                         }
93
94                         newCmdDefResult e = newCmdDef(name, def);
95                         switch (e) {
96                                 case CmdDefNameEmpty:
97                                         lexrc.printError("BN_DEFINE: Command name is empty");
98                                         error = true;
99                                         break;
100                                 case CmdDefExists:
101                                         lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
102                                         error = true;
103                                         break;
104                                 case CmdDefInvalid:
105                                         lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
106                                         error = true;
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         {
136                 func = FuncRequest::unknown;
137                 return false;
138         }
139
140         string const name2 = trim(name);
141
142         CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
143
144         if (pos == cmdDefMap.end()) 
145         {
146                 func = FuncRequest::unknown;
147                 return false;
148         }
149
150         if (pos->second->locked)
151         {
152                 func = FuncRequest::noaction;
153                 return false;
154         }
155
156         pos->second->locked = true;
157         func = pos->second->func;
158         return true;
159 }
160
161
162 void CmdDef::release(string const & name)
163 {
164         if (cmdDefMap.empty()) 
165                 return;
166
167         string const name2 = trim(name);
168
169         CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
170
171         if (pos == cmdDefMap.end()) 
172                 return;
173
174         pos->second->locked = false;
175 }
176
177 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name, 
178                                                                                   string const & def)
179 {
180         string const name2 = trim(name);
181
182         if (name2.empty()) 
183                 return CmdDefNameEmpty;
184
185         if (cmdDefMap.find(name) != cmdDefMap.end())
186                 return CmdDefExists;
187
188         FuncRequest     func = lyxaction.lookupFunc(def);
189         if (func.action == LFUN_NOACTION ||
190                 func.action == LFUN_UNKNOWN_ACTION) {
191                         return CmdDefInvalid;
192         }
193
194         boost::shared_ptr<CmdDefInfo> info;
195         info.reset(new CmdDefInfo(func));
196         cmdDefMap[name2] = info;
197
198         return CmdDefOk;
199 }
200
201
202 } // namespace lyx