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