]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommandparams.C
Preparation for the nomencl inset: Add {} in InsetCommand rather than
[lyx.git] / src / insets / insetcommandparams.C
1 /**
2  * \file insetcommandparams.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Georg Baum
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "insetcommandparams.h"
15
16 #include "debug.h"
17 #include "lyxlex.h"
18
19 #include "support/lstrings.h"
20
21 #include <boost/assert.hpp>
22
23
24 namespace lyx {
25
26 using support::findToken;
27
28 using std::string;
29 using std::endl;
30 using std::ostream;
31
32
33 InsetCommandParams::InsetCommandParams(string const & name)
34         : name_(name), preview_(false)
35 {
36         info_ = findInfo(name);
37         BOOST_ASSERT(info_);
38         params_.resize(info_->n);
39 }
40
41
42 InsetCommandParams::CommandInfo const *
43 InsetCommandParams::findInfo(std::string const & name)
44 {
45         // No parameter may be named "preview", because that is a required
46         // flag for all commands.
47
48         // InsetBibitem
49         if (name == "bibitem") {
50                 static const char * const paramnames[] = {"label", "key", ""};
51                 static const bool isoptional[] = {true, false};
52                 static const CommandInfo info = {2, paramnames, isoptional};
53                 return &info;
54         }
55
56         // InsetBibtex
57         if (name == "bibtex") {
58                 static const char * const paramnames[] =
59                                 {"options", "btprint", "bibfiles", ""};
60                 static const bool isoptional[] = {true, true, false};
61                 static const CommandInfo info = {3, paramnames, isoptional};
62                 return &info;
63         }
64
65         // InsetCitation
66         // FIXME: Use is_possible_cite_command() in
67         // src/frontends/controllers/biblio.C, see comment in src/factory.C.
68         if (name == "cite" || name == "citet" || name == "citep" || name == "citealt" ||
69             name == "citealp" || name == "citeauthor" || name == "citeyear" ||
70             name == "citeyearpar" || name == "citet*" || name == "citep*" ||
71             name == "citealt*" || name == "citealp*" ||
72             name == "citeauthor*" || name == "Citet" || name == "Citep" ||
73             name == "Citealt" || name == "Citealp" || name == "Citeauthor" ||
74             name == "Citet*" || name == "Citep*" || name == "Citealt*" ||
75             name == "Citealp*" || name == "Citeauthor*" ||
76             name == "citefield" || name == "citetitle" || name == "cite*") {
77                 // standard cite does only take one argument if jurabib is
78                 // not used, but jurabib extends this to two arguments, so
79                 // we have to allow both here. InsetCitation takes care that
80                 // LaTeX output is nevertheless correct.
81                 static const char * const paramnames[] =
82                                 {"after", "before", "key", ""};
83                 static const bool isoptional[] = {true, true, false};
84                 static const CommandInfo info = {3, paramnames, isoptional};
85                 return &info;
86         }
87
88         // InsetFloatlist
89         if (name == "floatlist") {
90                 static const char * const paramnames[] = {"type", ""};
91                 static const bool isoptional[] = {false};
92                 static const CommandInfo info = {1, paramnames, isoptional};
93                 return &info;
94         }
95
96         // InsetHfill
97         if (name == "hfill") {
98                 static const char * const paramnames[] = {""};
99                 static const CommandInfo info = {0, paramnames, 0};
100                 return &info;
101         }
102
103         // InsetInclude
104         if (name == "include" || name == "input" || name == "verbatiminput" ||
105             name == "verbatiminput*") {
106                 static const char * const paramnames[] = {"filename", ""};
107                 static const bool isoptional[] = {false};
108                 static const CommandInfo info = {1, paramnames, isoptional};
109                 return &info;
110         }
111
112         // InsetIndex, InsetPrintIndex, InsetLabel
113         if (name == "index" || name == "printindex" || name == "label") {
114                 static const char * const paramnames[] = {"name", ""};
115                 static const bool isoptional[] = {false};
116                 static const CommandInfo info = {1, paramnames, isoptional};
117                 return &info;
118         }
119
120         // InsetRef
121         if (name == "eqref" || name == "pageref" || name == "vpageref" ||
122             name == "vref" || name == "prettyref" || name == "ref") {
123                 static const char * const paramnames[] =
124                                 {"name", "reference", ""};
125                 static const bool isoptional[] = {true, false};
126                 static const CommandInfo info = {2, paramnames, isoptional};
127                 return &info;
128         }
129
130         // InsetTOC
131         if (name == "tableofcontents") {
132                 static const char * const paramnames[] = {"type", ""};
133                 static const bool isoptional[] = {false};
134                 static const CommandInfo info = {1, paramnames, isoptional};
135                 return &info;
136         }
137
138         // InsetUrl
139         if (name == "htmlurl" || name == "url") {
140                 static const char * const paramnames[] =
141                                 {"name", "target", ""};
142                 static const bool isoptional[] = {true, false};
143                 static const CommandInfo info = {2, paramnames, isoptional};
144                 return &info;
145         }
146
147         return 0;
148 }
149
150
151 void InsetCommandParams::setCmdName(string const & name)
152 {
153         name_ = name;
154         CommandInfo const * const info = findInfo(name);
155         BOOST_ASSERT(info);
156         ParamVector params(info->n);
157         // Overtake parameters with the same name
158         for (size_t i = 0; i < info_->n; ++i) {
159                 int j = findToken(info->paramnames, info_->paramnames[i]);
160                 if (j >= 0)
161                         params[j] = params_[i];
162         }
163         info_ = info;
164         std::swap(params, params_);
165 }
166
167
168 void InsetCommandParams::scanCommand(string const & cmd)
169 {
170         string tcmdname, toptions, tsecoptions, tcontents;
171
172         if (cmd.empty()) return;
173
174         enum { WS, CMDNAME, OPTION, SECOPTION, CONTENT } state = WS;
175
176         // Used to handle things like \command[foo[bar]]{foo{bar}}
177         int nestdepth = 0;
178
179         for (string::size_type i = 0; i < cmd.length(); ++i) {
180                 char const c = cmd[i];
181                 if ((state == CMDNAME && c == ' ') ||
182                     (state == CMDNAME && c == '[') ||
183                     (state == CMDNAME && c == '{')) {
184                         state = WS;
185                 }
186                 if ((state == OPTION  && c == ']') ||
187                     (state == SECOPTION  && c == ']') ||
188                     (state == CONTENT && c == '}')) {
189                         if (nestdepth == 0) {
190                                 state = WS;
191                         } else {
192                                 --nestdepth;
193                         }
194                 }
195                 if ((state == OPTION  && c == '[') ||
196                     (state == SECOPTION  && c == '[') ||
197                     (state == CONTENT && c == '{')) {
198                         ++nestdepth;
199                 }
200                 switch (state) {
201                 case CMDNAME:   tcmdname += c; break;
202                 case OPTION:    toptions += c; break;
203                 case SECOPTION: tsecoptions += c; break;
204                 case CONTENT:   tcontents += c; break;
205                 case WS: {
206                         char const b = i? cmd[i-1]: 0;
207                         if (c == '\\') {
208                                 state = CMDNAME;
209                         } else if (c == '[' && b != ']') {
210                                 state = OPTION;
211                                 nestdepth = 0; // Just to be sure
212                         } else if (c == '[' && b == ']') {
213                                 state = SECOPTION;
214                                 nestdepth = 0; // Just to be sure
215                         } else if (c == '{') {
216                                 state = CONTENT;
217                                 nestdepth = 0; // Just to be sure
218                         }
219                         break;
220                 }
221                 }
222         }
223
224         // Don't mess with this.
225         if (!tcmdname.empty())  setCmdName(tcmdname);
226         if (!toptions.empty())  setOptions(toptions);
227         if (!tsecoptions.empty())  setSecOptions(tsecoptions);
228         if (!tcontents.empty()) setContents(tcontents);
229
230         if (lyxerr.debugging(Debug::PARSER))
231                 lyxerr << "Command <" <<  cmd
232                        << "> == <" << to_utf8(getCommand())
233                        << "> == <" << getCmdName()
234                        << '|' << getContents()
235                        << '|' << getOptions()
236                        << '|' << getSecOptions() << '>' << endl;
237 }
238
239
240 void InsetCommandParams::read(LyXLex & lex)
241 {
242         if (lex.isOK()) {
243                 lex.next();
244                 name_ = lex.getString();
245                 info_ = findInfo(name_);
246                 if (!info_)
247                         lex.printError("InsetCommand: Unknown inset name `$$Token'");
248         }
249
250         string token;
251         while (lex.isOK()) {
252                 lex.next();
253                 token = lex.getString();
254                 if (token == "\\end_inset")
255                         break;
256                 // FIXME Why is preview_ read but not written?
257                 if (token == "preview") {
258                         lex.next();
259                         preview_ = lex.getBool();
260                         continue;
261                 }
262                 int const i = findToken(info_->paramnames, token);
263                 if (i >= 0) {
264                         lex.next(true);
265                         params_[i] = lex.getDocString();
266                 } else
267                         lex.printError("Unknown parameter name `$$Token' for command " + name_);
268         }
269         if (token != "\\end_inset") {
270                 lex.printError("Missing \\end_inset at this point. "
271                                "Read: `$$Token'");
272         }
273 }
274
275
276 void InsetCommandParams::write(ostream & os) const
277 {
278         os << "LatexCommand " << name_ << '\n';
279         for (size_t i = 0; i < info_->n; ++i)
280                 if (!params_[i].empty())
281                         // FIXME UNICODE
282                         os << info_->paramnames[i] << ' '
283                            << LyXLex::quoteString(to_utf8(params_[i]))
284                            << '\n';
285 }
286
287
288 docstring const InsetCommandParams::getCommand() const
289 {
290         docstring s = '\\' + from_ascii(name_);
291         for (size_t i = 0; i < info_->n; ++i) {
292                 if (info_->optional[i]) {
293                         if (params_[i].empty()) {
294                                 // We need to write this parameter even if
295                                 // it is empty if nonempty optional parameters
296                                 // follow before the next required parameter.
297                                 for (size_t j = i + 1; j < info_->n; ++j) {
298                                         if (!info_->optional[j])
299                                                 break;
300                                         if (!params_[j].empty()) {
301                                                 s += "[]";
302                                                 break;
303                                         }
304                                 }
305                         } else
306                                 s += '[' + params_[i] + ']';
307                 } else
308                         s += '{' + params_[i] + '}';
309         }
310         if (info_->n == 0)
311                 // Make sure that following stuff does not change the
312                 // command name.
313                 s += "{}";
314         return s;
315 }
316
317
318 std::string const InsetCommandParams::getOptions() const
319 {
320         for (size_t i = 0; i < info_->n; ++i)
321                 if (info_->optional[i])
322                         return to_utf8(params_[i]);
323         lyxerr << "Programming error: get nonexisting option in "
324                << name_ << " inset." << endl;; 
325         return string();
326 }
327
328
329 std::string const InsetCommandParams::getSecOptions() const
330 {
331         bool first = true;
332         for (size_t i = 0; i < info_->n; ++i)
333                 if (info_->optional[i]) {
334                         if (first)
335                                 first = false;
336                         else
337                                 return to_utf8(params_[i]);
338                 }
339         // Happens in InsetCitation
340         lyxerr << "Programming error: get nonexisting second option in "
341                << name_ << " inset." << endl;; 
342         return string();
343 }
344
345
346 std::string const InsetCommandParams::getContents() const
347 {
348         for (size_t i = 0; i < info_->n; ++i)
349                 if (!info_->optional[i])
350                         return to_utf8(params_[i]);
351         BOOST_ASSERT(false);
352         return string();
353 }
354
355
356 void InsetCommandParams::setOptions(std::string const & o)
357 {
358         for (size_t i = 0; i < info_->n; ++i)
359                 if (info_->optional[i]) {
360                         params_[i] = from_utf8(o);
361                         return;
362                 }
363         lyxerr << "Programming error: set nonexisting option in "
364                << name_ << " inset." << endl;; 
365 }
366
367
368 void InsetCommandParams::setSecOptions(std::string const & s)
369 {
370         bool first = true;
371         for (size_t i = 0; i < info_->n; ++i)
372                 if (info_->optional[i]) {
373                         if (first)
374                                 first = false;
375                         else {
376                                 params_[i] = from_utf8(s);
377                                 return;
378                         }
379                 }
380         // Happens in InsetCitation
381         lyxerr << "Programming error: set nonexisting second option in "
382                << name_ << " inset." << endl;; 
383 }
384
385
386 void InsetCommandParams::setContents(std::string const & c)
387 {
388         for (size_t i = 0; i < info_->n; ++i)
389                 if (!info_->optional[i]) {
390                         params_[i] = from_utf8(c);
391                         return;
392                 }
393         BOOST_ASSERT(false);
394 }
395
396
397 docstring const & InsetCommandParams::operator[](string const & name) const
398 {
399         int const i = findToken(info_->paramnames, name);
400         BOOST_ASSERT(i >= 0);
401         return params_[i];
402 }
403
404
405 docstring & InsetCommandParams::operator[](string const & name)
406 {
407         int const i = findToken(info_->paramnames, name);
408         BOOST_ASSERT(i >= 0);
409         return params_[i];
410 }
411
412
413 void InsetCommandParams::clear()
414 {
415         for (size_t i = 0; i < info_->n; ++i)
416                 params_[i].clear();
417 }
418
419
420 bool operator==(InsetCommandParams const & o1,
421                 InsetCommandParams const & o2)
422 {
423         return o1.name_ == o2.name_ &&
424                o1.info_ == o2.info_ &&
425                o1.params_ == o2.params_ &&
426                o1.preview_ == o2.preview_;
427 }
428
429
430 bool operator!=(InsetCommandParams const & o1,
431                 InsetCommandParams const & o2)
432 {
433         return !(o1 == o2);
434 }
435
436
437 } // namespace lyx