]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.cpp
Cmake tests: macro setmarkedtestlabel() worked only by chance
[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 "Buffer.h"
32 #include "Encoding.h"
33 #include "Lexer.h"
34 #include "OutputParams.h"
35
36 #include "frontends/alert.h"
37
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43
44 #include "support/lassert.h"
45
46 using namespace std;
47 using namespace lyx::support;
48
49
50 namespace lyx {
51
52 /// Get information for \p code and command \p cmdName.
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                 LATTEST(false);
86                 // fall through in release mode
87         }
88         static const ParamInfo pi;
89         return pi;
90 }
91
92
93 /////////////////////////////////////////////////////////////////////
94 //
95 // ParamInfo::ParamData
96 //
97 /////////////////////////////////////////////////////////////////////
98
99 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
100                                 ParamHandling h)
101         : name_(s), type_(t), handling_(h)
102 {}
103
104
105 bool ParamInfo::ParamData::isOptional() const
106 {
107         return type_ == ParamInfo::LATEX_OPTIONAL;
108 }
109
110
111 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
112 {
113         return name() == rhs.name() && type() == rhs.type()
114                 && handling() == rhs.handling();
115 }
116
117
118 bool ParamInfo::hasParam(std::string const & name) const
119 {
120         const_iterator it = begin();
121         const_iterator last = end();
122         for (; it != last; ++it) {
123                 if (it->name() == name)
124                         return true;
125         }
126         return false;
127 }
128
129
130 void ParamInfo::add(std::string const & name, ParamType type,
131                     ParamHandling handling)
132
133         info_.push_back(ParamData(name, type, handling)); 
134 }
135
136
137 bool ParamInfo::operator==(ParamInfo const & rhs) const
138 {
139         if (size() != rhs.size())
140                 return false;
141         return equal(begin(), end(), rhs.begin());
142 }
143
144
145 ParamInfo::ParamData const & 
146         ParamInfo::operator[](std::string const & name) const
147 {
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         LATTEST(false);
155         // we will try to continue in release mode
156         static const ParamData pd("asdfghjkl", LYX_INTERNAL);
157         return pd;
158 }
159
160
161 /////////////////////////////////////////////////////////////////////
162 //
163 // InsetCommandParams
164 //
165 /////////////////////////////////////////////////////////////////////
166
167
168 InsetCommandParams::InsetCommandParams(InsetCode code)
169         : insetCode_(code), preview_(false)
170 {
171         cmdName_ = getDefaultCmd(code);
172         info_ = findInfo(code, cmdName_);
173 }
174
175
176 InsetCommandParams::InsetCommandParams(InsetCode code,
177         string const & cmdName)
178         : insetCode_(code), cmdName_(cmdName), preview_(false)
179 {
180         info_ = findInfo(code, cmdName);
181 }
182
183
184 std::string InsetCommandParams::insetType() const
185 {
186         return insetName(insetCode_);
187 }
188
189
190 string InsetCommandParams::getDefaultCmd(InsetCode code)
191 {
192         switch (code) {
193                 case BIBITEM_CODE: 
194                         return InsetBibitem::defaultCommand();
195                 case BIBTEX_CODE:
196                         return InsetBibtex::defaultCommand();
197                 case CITE_CODE:
198                         return InsetCitation::defaultCommand();
199                 case FLOAT_LIST_CODE:
200                         return InsetFloatList::defaultCommand();
201                 case HYPERLINK_CODE:
202                         return InsetHyperlink::defaultCommand();
203                 case INCLUDE_CODE:
204                         return InsetInclude::defaultCommand();
205                 case INDEX_PRINT_CODE:
206                         return InsetPrintIndex::defaultCommand();
207                 case LABEL_CODE:
208                         return InsetLabel::defaultCommand();
209                 case LINE_CODE:
210                         return InsetLine::defaultCommand();
211                 case NOMENCL_CODE:
212                         return InsetNomencl::defaultCommand();
213                 case NOMENCL_PRINT_CODE:
214                         return InsetPrintNomencl::defaultCommand();
215                 case REF_CODE:
216                         return InsetRef::defaultCommand();
217                 case TOC_CODE:
218                         return InsetTOC::defaultCommand();
219                 default:
220                         LATTEST(false);
221                         // fall through in release mode
222         }
223         return string();
224 }
225
226
227 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
228 {
229         switch (code) {
230                 case BIBITEM_CODE: 
231                         return InsetBibitem::isCompatibleCommand(s);
232                 case BIBTEX_CODE:
233                         return InsetBibtex::isCompatibleCommand(s);
234                 case CITE_CODE:
235                         return InsetCitation::isCompatibleCommand(s);
236                 case FLOAT_LIST_CODE:
237                         return InsetFloatList::isCompatibleCommand(s);
238                 case HYPERLINK_CODE:
239                         return InsetHyperlink::isCompatibleCommand(s);
240                 case INCLUDE_CODE:
241                         return InsetInclude::isCompatibleCommand(s);
242                 case INDEX_PRINT_CODE:
243                         return InsetPrintIndex::isCompatibleCommand(s);
244                 case LABEL_CODE:
245                         return InsetLabel::isCompatibleCommand(s);
246                 case LINE_CODE:
247                         return InsetLine::isCompatibleCommand(s);
248                 case NOMENCL_CODE:
249                         return InsetNomencl::isCompatibleCommand(s);
250                 case NOMENCL_PRINT_CODE:
251                         return InsetPrintNomencl::isCompatibleCommand(s);
252                 case REF_CODE:
253                         return InsetRef::isCompatibleCommand(s);
254                 case TOC_CODE:
255                         return InsetTOC::isCompatibleCommand(s);
256         default:
257                 LATTEST(false);
258                 // fall through in release mode
259         }
260         return false;
261 }
262
263
264 void InsetCommandParams::setCmdName(string const & name)
265 {
266         if (!isCompatibleCommand(insetCode_, name)) {
267                 LYXERR0("InsetCommand: Incompatible command name " << 
268                                 name << ".");
269                 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
270                                        _("Incompatible command name."));
271         }
272
273         cmdName_ = name;
274         info_ = findInfo(insetCode_, cmdName_);
275 }
276
277
278 void InsetCommandParams::read(Lexer & lex)
279 {
280         Read(lex, 0);
281 }
282
283
284 void InsetCommandParams::Read(Lexer & lex, Buffer const * buffer)
285 {
286         lex.setContext("InsetCommandParams::read");
287         lex >> insetName(insetCode_).c_str();
288         lex >> "LatexCommand";
289         lex >> cmdName_;
290         if (!isCompatibleCommand(insetCode_, cmdName_)) {
291                 lex.printError("Incompatible command name " + cmdName_ + ".");
292                 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
293                                        _("Incompatible command name."));
294         }
295
296         info_ = findInfo(insetCode_, cmdName_);
297         
298         string token;
299         while (lex.isOK()) {
300                 lex.next();
301                 token = lex.getString();
302                 if (token == "\\end_inset")
303                         break;
304                 if (token == "preview") {
305                         lex.next();
306                         preview_ = lex.getBool();
307                         continue;
308                 }
309                 if (info_.hasParam(token)) {
310                         lex.next(true);
311                         docstring data = lex.getDocString();
312                         if (buffer && token == "filename") {
313                                 data = from_utf8(buffer->includedFilePath(to_utf8(data)));
314                         } else if (buffer && token == "bibfiles") {
315                                 int i = 0;
316                                 docstring newdata;
317                                 docstring bib = support::token(data, ',', i);
318                                 while (!bib.empty()) {
319                                         bib = from_utf8(buffer->includedFilePath(to_utf8(bib), "bib"));
320                                         if (!newdata.empty())
321                                                 newdata.append(1, ',');
322                                         newdata.append(bib);
323                                         bib = support::token(data, ',', ++i);
324                                 }
325                                 data = newdata;
326                         } else if (buffer && token == "options") {
327                                 data = from_utf8(buffer->includedFilePath(to_utf8(data), "bst"));
328                         }
329                         params_[token] = data;
330                 } else {
331                         lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
332                         throw ExceptionMessage(WarningException,
333                                 _("InsetCommandParams: ") + from_ascii(cmdName_),
334                                 _("Unknown parameter name: ") + from_utf8(token));
335                 }
336         }
337         if (token != "\\end_inset") {
338                 lex.printError("Missing \\end_inset at this point. "
339                                "Read: `$$Token'");
340                 throw ExceptionMessage(WarningException,
341                         _("InsetCommandParams Error: "),
342                         _("Missing \\end_inset at this point: ") + from_utf8(token));
343         }
344 }
345
346
347 void InsetCommandParams::write(ostream & os) const
348 {
349         Write(os, 0);
350 }
351
352
353 void InsetCommandParams::Write(ostream & os, Buffer const * buffer) const
354 {
355         os << "CommandInset " << insetType() << '\n';
356         os << "LatexCommand " << cmdName_ << '\n';
357         if (preview_)
358                 os << "preview true\n";
359         ParamInfo::const_iterator it  = info_.begin();
360         ParamInfo::const_iterator end = info_.end();
361         for (; it != end; ++it) {
362                 string const & name = it->name();
363                 string data = to_utf8((*this)[name]);
364                 if (!data.empty()) {
365                         // Adjust path of files if document was moved
366                         if (buffer && name == "filename") {
367                                 data = buffer->includedFilePath(data);
368                         } else if (buffer && name == "bibfiles") {
369                                 int i = 0;
370                                 string newdata;
371                                 string bib = token(data, ',', i);
372                                 while (!bib.empty()) {
373                                         bib = buffer->includedFilePath(bib, "bib");
374                                         if (!newdata.empty())
375                                                 newdata.append(1, ',');
376                                         newdata.append(bib);
377                                         bib = token(data, ',', ++i);
378                                 }
379                                 data = newdata;
380                         } else if (buffer && name == "options") {
381                                 data = buffer->includedFilePath(data, "bst");
382                         }
383                         os << name << ' '
384                            << Lexer::quoteString(data)
385                            << '\n';
386                 }
387         }
388 }
389
390
391 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
392 {
393         LASSERT(ci->isOptional(), return false);
394
395         ++ci; // we want to start with the next one
396         ParamInfo::const_iterator end = info_.end();
397         for (; ci != end; ++ci) {
398                 switch (ci->type()) {
399                 case ParamInfo::LYX_INTERNAL:
400                         break;
401
402                 case ParamInfo::LATEX_REQUIRED:
403                         return false;
404
405                 case ParamInfo::LATEX_OPTIONAL: {
406                         std::string const & name = ci->name();
407                         docstring const & data = (*this)[name];
408                         if (!data.empty())
409                                 return true;
410                         break;
411                 }
412
413                 } //end switch
414         }
415         return false;
416 }
417
418
419 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
420                                              docstring const & command,
421                                              ParamInfo::ParamHandling handling) const
422 {
423         docstring result;
424         switch (handling) {
425         case ParamInfo::HANDLING_LATEXIFY: {
426                 pair<docstring, docstring> command_latexed =
427                         runparams.encoding->latexString(command, runparams.dryrun);
428                 result = command_latexed.first;
429                 if (!command_latexed.second.empty()) {
430                         // issue a warning about omitted characters
431                         // FIXME: should be passed to the error dialog
432                         frontend::Alert::warning(_("Uncodable characters"),
433                                 bformat(_("The following characters that are used in the inset %1$s are not\n"
434                                           "representable in the current encoding and therefore have been omitted:\n%2$s."),
435                                         from_utf8(insetType()), command_latexed.second));
436                 }
437                 break;
438         } 
439         case ParamInfo::HANDLING_ESCAPE:
440                 result = escape(command);
441                 break;
442         case ParamInfo::HANDLING_NONE:
443                 result = command;
444                 break;
445         } // switch
446
447         return result;
448 }
449
450
451 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
452 {
453         docstring s = '\\' + from_ascii(cmdName_);
454         bool noparam = true;
455         ParamInfo::const_iterator it  = info_.begin();
456         ParamInfo::const_iterator end = info_.end();
457         for (; it != end; ++it) {
458                 std::string const & name = it->name();
459                 switch (it->type()) {
460                 case ParamInfo::LYX_INTERNAL:
461                         break;
462
463                 case ParamInfo::LATEX_REQUIRED: {
464                         docstring const data =
465                                 prepareCommand(runparams, (*this)[name], it->handling());
466                         s += '{' + data + '}';
467                         noparam = false;
468                         break;
469                 }
470                 case ParamInfo::LATEX_OPTIONAL: {
471                         docstring const data =
472                                 prepareCommand(runparams, (*this)[name], it->handling());
473                         if (!data.empty()) {
474                                 s += '[' + data + ']';
475                                 noparam = false;
476                         } else if (writeEmptyOptional(it)) {
477                                         s += "[]";
478                                         noparam = false;
479                         }
480                         break;
481                 } 
482                 } //end switch
483         }
484         if (noparam)
485                 // Make sure that following stuff does not change the
486                 // command name.
487                 s += "{}";
488         return s;
489 }
490
491
492 docstring InsetCommandParams::getFirstNonOptParam() const
493 {
494         ParamInfo::const_iterator it = 
495                 find_if(info_.begin(), info_.end(), 
496                         not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
497         LASSERT(it != info_.end(), return docstring());
498         return (*this)[it->name()];
499 }
500
501
502 docstring const & InsetCommandParams::operator[](string const & name) const
503 {
504         static const docstring dummy;
505         LASSERT(info_.hasParam(name), return dummy);
506         ParamMap::const_iterator data = params_.find(name);
507         if (data == params_.end() || data->second.empty())
508                 return dummy;
509         return data->second;
510 }
511
512
513 docstring & InsetCommandParams::operator[](string const & name)
514 {
515         LATTEST(info_.hasParam(name));
516         // this will add the name in release mode
517         return params_[name];
518 }
519
520
521 void InsetCommandParams::clear()
522 {
523         params_.clear();
524 }
525
526
527 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
528 {
529         return o1.insetCode_ == o2.insetCode_
530                 && o1.cmdName_ == o2.cmdName_
531                 && o1.info_ == o2.info_
532                 && o1.params_ == o2.params_
533                 && o1.preview_ == o2.preview_;
534 }
535
536
537 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)
538 {
539         return !(o1 == o2);
540 }
541
542
543 } // namespace lyx