]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.cpp
tex2lyx: support for Spreadsheet and chess external templates
[lyx.git] / src / insets / InsetCommandParams.cpp
1 /**
2  * \file InsetCommandParams.cpp
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  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14 #include <algorithm>
15
16 #include "InsetCommandParams.h"
17
18 #include "InsetBibitem.h"
19 #include "InsetBibtex.h"
20 #include "InsetCitation.h"
21 #include "InsetFloatList.h"
22 #include "InsetHyperlink.h"
23 #include "InsetInclude.h"
24 #include "InsetIndex.h"
25 #include "InsetLabel.h"
26 #include "InsetLine.h"
27 #include "InsetNomencl.h"
28 #include "InsetRef.h"
29 #include "InsetTOC.h"
30
31 #include "Encoding.h"
32 #include "Lexer.h"
33 #include "OutputParams.h"
34
35 #include "frontends/alert.h"
36
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/ExceptionMessage.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42
43 #include "support/lassert.h"
44
45 using namespace std;
46 using namespace lyx::support;
47
48
49 namespace lyx {
50
51 /// Get information for \p code and command \p cmdName.
52 /// Returns 0 if the combination is not known.  [FIXME: 0?]
53 /// Don't call this without first making sure the command name is
54 /// acceptable to the inset.
55 static ParamInfo const & findInfo(InsetCode code, string const & cmdName)
56 {
57         switch (code) {
58         case BIBITEM_CODE:
59                 return InsetBibitem::findInfo(cmdName);
60         case BIBTEX_CODE:
61                 return InsetBibtex::findInfo(cmdName);
62         case CITE_CODE:
63                 return InsetCitation::findInfo(cmdName);
64         case FLOAT_LIST_CODE:
65                 return InsetFloatList::findInfo(cmdName);
66         case HYPERLINK_CODE:
67                 return InsetHyperlink::findInfo(cmdName);
68         case INCLUDE_CODE:
69                 return InsetInclude::findInfo(cmdName);
70         case INDEX_PRINT_CODE:
71                 return InsetPrintIndex::findInfo(cmdName);
72         case LABEL_CODE:
73                 return InsetLabel::findInfo(cmdName);
74         case LINE_CODE:
75                 return InsetLine::findInfo(cmdName);
76         case NOMENCL_CODE:
77                 return InsetNomencl::findInfo(cmdName);
78         case NOMENCL_PRINT_CODE:
79                 return InsetPrintNomencl::findInfo(cmdName);
80         case REF_CODE:
81                 return InsetRef::findInfo(cmdName);
82         case TOC_CODE:
83                 return InsetTOC::findInfo(cmdName);
84         default:
85                 LASSERT(false, /**/);
86         }
87         static ParamInfo pi;
88         return pi; // to silence the warning
89 }
90
91
92 /////////////////////////////////////////////////////////////////////
93 //
94 // ParamInfo::ParamData
95 //
96 /////////////////////////////////////////////////////////////////////
97
98 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
99                                 ParamHandling h)
100         : name_(s), type_(t), handling_(h)
101 {}
102
103
104 bool ParamInfo::ParamData::isOptional() const
105 {
106         return type_ == ParamInfo::LATEX_OPTIONAL;
107 }
108
109
110 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
111 {
112         return name() == rhs.name() && type() == rhs.type()
113                 && handling() == rhs.handling();
114 }
115
116
117 bool ParamInfo::hasParam(std::string const & name) const
118 {
119         const_iterator it = begin();
120         const_iterator last = end();
121         for (; it != last; ++it) {
122                 if (it->name() == name)
123                         return true;
124         }
125         return false;
126 }
127
128
129 void ParamInfo::add(std::string const & name, ParamType type,
130                     ParamHandling handling)
131
132         info_.push_back(ParamData(name, type, handling)); 
133 }
134
135
136 bool ParamInfo::operator==(ParamInfo const & rhs) const
137 {
138         if (size() != rhs.size())
139                 return false;
140         return equal(begin(), end(), rhs.begin());
141 }
142
143
144 ParamInfo::ParamData const & 
145         ParamInfo::operator[](std::string const & name) const
146 {
147         LASSERT(hasParam(name), /**/);
148         const_iterator it = begin();
149         const_iterator last = end();
150         for (; it != last; ++it) {
151                 if (it->name() == name)
152                         return *it;
153         }
154         return *it; // silence warning
155 }
156
157
158 /////////////////////////////////////////////////////////////////////
159 //
160 // InsetCommandParams
161 //
162 /////////////////////////////////////////////////////////////////////
163
164
165 InsetCommandParams::InsetCommandParams(InsetCode code)
166         : insetCode_(code), preview_(false)
167 {
168         cmdName_ = getDefaultCmd(code);
169         info_ = findInfo(code, cmdName_);
170 }
171
172
173 InsetCommandParams::InsetCommandParams(InsetCode code,
174         string const & cmdName)
175         : insetCode_(code), cmdName_(cmdName), preview_(false)
176 {
177         info_ = findInfo(code, cmdName);
178 }
179
180
181 std::string InsetCommandParams::insetType() const
182 {
183         return insetName(insetCode_);
184 }
185
186
187 string InsetCommandParams::getDefaultCmd(InsetCode code)
188 {
189         switch (code) {
190                 case BIBITEM_CODE: 
191                         return InsetBibitem::defaultCommand();
192                 case BIBTEX_CODE:
193                         return InsetBibtex::defaultCommand();
194                 case CITE_CODE:
195                         return InsetCitation::defaultCommand();
196                 case FLOAT_LIST_CODE:
197                         return InsetFloatList::defaultCommand();
198                 case HYPERLINK_CODE:
199                         return InsetHyperlink::defaultCommand();
200                 case INCLUDE_CODE:
201                         return InsetInclude::defaultCommand();
202                 case INDEX_PRINT_CODE:
203                         return InsetPrintIndex::defaultCommand();
204                 case LABEL_CODE:
205                         return InsetLabel::defaultCommand();
206                 case LINE_CODE:
207                         return InsetLine::defaultCommand();
208                 case NOMENCL_CODE:
209                         return InsetNomencl::defaultCommand();
210                 case NOMENCL_PRINT_CODE:
211                         return InsetPrintNomencl::defaultCommand();
212                 case REF_CODE:
213                         return InsetRef::defaultCommand();
214                 case TOC_CODE:
215                         return InsetTOC::defaultCommand();
216                 default:
217                         LASSERT(false, /**/);
218         }
219         return string(); // silence the warning
220 }
221
222
223 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
224 {
225         switch (code) {
226                 case BIBITEM_CODE: 
227                         return InsetBibitem::isCompatibleCommand(s);
228                 case BIBTEX_CODE:
229                         return InsetBibtex::isCompatibleCommand(s);
230                 case CITE_CODE:
231                         return InsetCitation::isCompatibleCommand(s);
232                 case FLOAT_LIST_CODE:
233                         return InsetFloatList::isCompatibleCommand(s);
234                 case HYPERLINK_CODE:
235                         return InsetHyperlink::isCompatibleCommand(s);
236                 case INCLUDE_CODE:
237                         return InsetInclude::isCompatibleCommand(s);
238                 case INDEX_PRINT_CODE:
239                         return InsetPrintIndex::isCompatibleCommand(s);
240                 case LABEL_CODE:
241                         return InsetLabel::isCompatibleCommand(s);
242                 case LINE_CODE:
243                         return InsetLine::isCompatibleCommand(s);
244                 case NOMENCL_CODE:
245                         return InsetNomencl::isCompatibleCommand(s);
246                 case NOMENCL_PRINT_CODE:
247                         return InsetPrintNomencl::isCompatibleCommand(s);
248                 case REF_CODE:
249                         return InsetRef::isCompatibleCommand(s);
250                 case TOC_CODE:
251                         return InsetTOC::isCompatibleCommand(s);
252                 default:
253                         LASSERT(false, /**/);
254         }
255         return false; // silence the warning
256 }
257
258
259 void InsetCommandParams::setCmdName(string const & name)
260 {
261         if (!isCompatibleCommand(insetCode_, name)) {
262                 LYXERR0("InsetCommand: Incompatible command name " << 
263                                 name << ".");
264                 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
265                                        _("Incompatible command name."));
266         }
267
268         cmdName_ = name;
269         info_ = findInfo(insetCode_, cmdName_);
270 }
271
272
273 void InsetCommandParams::read(Lexer & lex)
274 {
275         lex.setContext("InsetCommandParams::read");
276         lex >> insetName(insetCode_).c_str();
277         lex >> "LatexCommand";
278         lex >> cmdName_;
279         if (!isCompatibleCommand(insetCode_, cmdName_)) {
280                 lex.printError("Incompatible command name " + cmdName_ + ".");
281                 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
282                                        _("Incompatible command name."));
283         }
284
285         info_ = findInfo(insetCode_, cmdName_);
286         
287         string token;
288         while (lex.isOK()) {
289                 lex.next();
290                 token = lex.getString();
291                 if (token == "\\end_inset")
292                         break;
293                 if (token == "preview") {
294                         lex.next();
295                         preview_ = lex.getBool();
296                         continue;
297                 }
298                 if (info_.hasParam(token)) {
299                         lex.next(true);
300                         params_[token] = lex.getDocString();
301                 } else {
302                         lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
303                         throw ExceptionMessage(WarningException,
304                                 _("InsetCommandParams: ") + from_ascii(cmdName_),
305                                 _("Unknown parameter name: ") + from_utf8(token));
306                 }
307         }
308         if (token != "\\end_inset") {
309                 lex.printError("Missing \\end_inset at this point. "
310                                "Read: `$$Token'");
311                 throw ExceptionMessage(WarningException,
312                         _("InsetCommandParams Error: "),
313                         _("Missing \\end_inset at this point: ") + from_utf8(token));
314         }
315 }
316
317
318 void InsetCommandParams::write(ostream & os) const
319 {
320         os << "CommandInset " << insetType() << '\n';
321         os << "LatexCommand " << cmdName_ << '\n';
322         if (preview_)
323                 os << "preview true\n";
324         ParamInfo::const_iterator it  = info_.begin();
325         ParamInfo::const_iterator end = info_.end();
326         for (; it != end; ++it) {
327                 std::string const & name = it->name();
328                 docstring const & data = (*this)[name];
329                 if (!data.empty()) {
330                         // FIXME UNICODE
331                         os << name << ' '
332                            << Lexer::quoteString(to_utf8(data))
333                            << '\n';
334                 }
335         }
336 }
337
338
339 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
340 {
341         if (!ci->isOptional()) {
342                 LASSERT(false, /**/);
343         }
344         ++ci; // we want to start with the next one
345         ParamInfo::const_iterator end = info_.end();
346         for (; ci != end; ++ci) {
347                 switch (ci->type()) {
348                 case ParamInfo::LYX_INTERNAL:
349                         break;
350
351                 case ParamInfo::LATEX_REQUIRED:
352                         return false;
353
354                 case ParamInfo::LATEX_OPTIONAL: {
355                         std::string const & name = ci->name();
356                         docstring const & data = (*this)[name];
357                         if (!data.empty())
358                                 return true;
359                         break;
360                 }
361
362                 } //end switch
363         }
364         return false;
365 }
366
367
368 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
369                                              docstring const & command,
370                                              ParamInfo::ParamHandling handling) const
371 {
372         docstring result;
373         switch (handling) {
374         case ParamInfo::HANDLING_LATEXIFY: {
375                 docstring uncodable;
376                 for (size_t n = 0; n < command.size(); ++n) {
377                         try {
378                                 char_type const c = command[n];
379                                 docstring const latex = runparams.encoding->latexChar(c);
380                                 result += latex;
381                                 if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
382                                         // Prevent eating of a following
383                                         // space or command corruption by
384                                         // following characters
385                                         result +=  "{}";
386                                 }
387                         } catch (EncodingException & /* e */) {
388                                 LYXERR0("Uncodable character in command inset!");
389                                 if (runparams.dryrun) {
390                                         result += "<" + _("LyX Warning: ")
391                                                 + _("uncodable character") + " '";
392                                         result += docstring(1, command[n]);
393                                         result += "'>";
394                                 } else
395                                         uncodable += command[n];
396                         }
397                 }
398                 if (!uncodable.empty()) {
399                         // issue a warning about omitted characters
400                         // FIXME: should be passed to the error dialog
401                         frontend::Alert::warning(_("Uncodable characters"),
402                                 bformat(_("The following characters that are used in the inset %1$s are not\n"
403                                           "representable in the current encoding and therefore have been omitted:\n%2$s."),
404                                         from_utf8(insetType()), uncodable));
405                 }
406                 break;
407         } 
408         case ParamInfo::HANDLING_ESCAPE:
409                 result = escape(command);
410                 break;
411         case ParamInfo::HANDLING_NONE:
412                 result = command;
413                 break;
414         } // switch
415
416         return result;
417 }
418
419
420 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
421 {
422         docstring s = '\\' + from_ascii(cmdName_);
423         bool noparam = true;
424         ParamInfo::const_iterator it  = info_.begin();
425         ParamInfo::const_iterator end = info_.end();
426         for (; it != end; ++it) {
427                 std::string const & name = it->name();
428                 switch (it->type()) {
429                 case ParamInfo::LYX_INTERNAL:
430                         break;
431
432                 case ParamInfo::LATEX_REQUIRED: {
433                         docstring const data =
434                                 prepareCommand(runparams, (*this)[name], it->handling());
435                         s += '{' + data + '}';
436                         noparam = false;
437                         break;
438                 }
439                 case ParamInfo::LATEX_OPTIONAL: {
440                         docstring const data =
441                                 prepareCommand(runparams, (*this)[name], it->handling());
442                         if (!data.empty()) {
443                                 s += '[' + data + ']';
444                                 noparam = false;
445                         } else if (writeEmptyOptional(it)) {
446                                         s += "[]";
447                                         noparam = false;
448                         }
449                         break;
450                 } 
451                 } //end switch
452         }
453         if (noparam)
454                 // Make sure that following stuff does not change the
455                 // command name.
456                 s += "{}";
457         return s;
458 }
459
460
461 docstring InsetCommandParams::getFirstNonOptParam() const
462 {
463         ParamInfo::const_iterator it = 
464                 find_if(info_.begin(), info_.end(), 
465                         not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
466         if (it == info_.end()) {
467                 LASSERT(false, return docstring());
468         }
469         return (*this)[it->name()];
470 }
471
472
473 docstring const & InsetCommandParams::operator[](string const & name) const
474 {
475         static const docstring dummy; //so we don't return a ref to temporary
476         LASSERT(info_.hasParam(name), return dummy);
477         ParamMap::const_iterator data = params_.find(name);
478         if (data == params_.end() || data->second.empty())
479                 return dummy;
480         return data->second;
481 }
482
483
484 docstring & InsetCommandParams::operator[](string const & name)
485 {
486         LASSERT(info_.hasParam(name), /**/);
487         return params_[name];
488 }
489
490
491 void InsetCommandParams::clear()
492 {
493         params_.clear();
494 }
495
496
497 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
498 {
499         return o1.insetCode_ == o2.insetCode_
500                 && o1.cmdName_ == o2.cmdName_
501                 && o1.info_ == o2.info_
502                 && o1.params_ == o2.params_
503                 && o1.preview_ == o2.preview_;
504 }
505
506
507 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)
508 {
509         return !(o1 == o2);
510 }
511
512
513 } // namespace lyx