]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.cpp
Fix crash after undo following replacement of a multicell selection (#8973).
[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 /// Don't call this without first making sure the command name is
53 /// acceptable to the inset.
54 static ParamInfo const & findInfo(InsetCode code, string const & cmdName)
55 {
56         switch (code) {
57         case BIBITEM_CODE:
58                 return InsetBibitem::findInfo(cmdName);
59         case BIBTEX_CODE:
60                 return InsetBibtex::findInfo(cmdName);
61         case CITE_CODE:
62                 return InsetCitation::findInfo(cmdName);
63         case FLOAT_LIST_CODE:
64                 return InsetFloatList::findInfo(cmdName);
65         case HYPERLINK_CODE:
66                 return InsetHyperlink::findInfo(cmdName);
67         case INCLUDE_CODE:
68                 return InsetInclude::findInfo(cmdName);
69         case INDEX_PRINT_CODE:
70                 return InsetPrintIndex::findInfo(cmdName);
71         case LABEL_CODE:
72                 return InsetLabel::findInfo(cmdName);
73         case LINE_CODE:
74                 return InsetLine::findInfo(cmdName);
75         case NOMENCL_CODE:
76                 return InsetNomencl::findInfo(cmdName);
77         case NOMENCL_PRINT_CODE:
78                 return InsetPrintNomencl::findInfo(cmdName);
79         case REF_CODE:
80                 return InsetRef::findInfo(cmdName);
81         case TOC_CODE:
82                 return InsetTOC::findInfo(cmdName);
83         default:
84                 LATTEST(false);
85                 // fall through in release mode
86         }
87         static const ParamInfo pi;
88         return pi;
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         const_iterator it = begin();
148         const_iterator last = end();
149         for (; it != last; ++it) {
150                 if (it->name() == name)
151                         return *it;
152         }
153         LATTEST(false);
154         // we will try to continue in release mode
155         static const ParamData pd("asdfghjkl", LYX_INTERNAL);
156         return pd;
157 }
158
159
160 /////////////////////////////////////////////////////////////////////
161 //
162 // InsetCommandParams
163 //
164 /////////////////////////////////////////////////////////////////////
165
166
167 InsetCommandParams::InsetCommandParams(InsetCode code)
168         : insetCode_(code), preview_(false)
169 {
170         cmdName_ = getDefaultCmd(code);
171         info_ = findInfo(code, cmdName_);
172 }
173
174
175 InsetCommandParams::InsetCommandParams(InsetCode code,
176         string const & cmdName)
177         : insetCode_(code), cmdName_(cmdName), preview_(false)
178 {
179         info_ = findInfo(code, cmdName);
180 }
181
182
183 std::string InsetCommandParams::insetType() const
184 {
185         return insetName(insetCode_);
186 }
187
188
189 string InsetCommandParams::getDefaultCmd(InsetCode code)
190 {
191         switch (code) {
192                 case BIBITEM_CODE: 
193                         return InsetBibitem::defaultCommand();
194                 case BIBTEX_CODE:
195                         return InsetBibtex::defaultCommand();
196                 case CITE_CODE:
197                         return InsetCitation::defaultCommand();
198                 case FLOAT_LIST_CODE:
199                         return InsetFloatList::defaultCommand();
200                 case HYPERLINK_CODE:
201                         return InsetHyperlink::defaultCommand();
202                 case INCLUDE_CODE:
203                         return InsetInclude::defaultCommand();
204                 case INDEX_PRINT_CODE:
205                         return InsetPrintIndex::defaultCommand();
206                 case LABEL_CODE:
207                         return InsetLabel::defaultCommand();
208                 case LINE_CODE:
209                         return InsetLine::defaultCommand();
210                 case NOMENCL_CODE:
211                         return InsetNomencl::defaultCommand();
212                 case NOMENCL_PRINT_CODE:
213                         return InsetPrintNomencl::defaultCommand();
214                 case REF_CODE:
215                         return InsetRef::defaultCommand();
216                 case TOC_CODE:
217                         return InsetTOC::defaultCommand();
218                 default:
219                         LATTEST(false);
220                         // fall through in release mode
221         }
222         return string();
223 }
224
225
226 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
227 {
228         switch (code) {
229                 case BIBITEM_CODE: 
230                         return InsetBibitem::isCompatibleCommand(s);
231                 case BIBTEX_CODE:
232                         return InsetBibtex::isCompatibleCommand(s);
233                 case CITE_CODE:
234                         return InsetCitation::isCompatibleCommand(s);
235                 case FLOAT_LIST_CODE:
236                         return InsetFloatList::isCompatibleCommand(s);
237                 case HYPERLINK_CODE:
238                         return InsetHyperlink::isCompatibleCommand(s);
239                 case INCLUDE_CODE:
240                         return InsetInclude::isCompatibleCommand(s);
241                 case INDEX_PRINT_CODE:
242                         return InsetPrintIndex::isCompatibleCommand(s);
243                 case LABEL_CODE:
244                         return InsetLabel::isCompatibleCommand(s);
245                 case LINE_CODE:
246                         return InsetLine::isCompatibleCommand(s);
247                 case NOMENCL_CODE:
248                         return InsetNomencl::isCompatibleCommand(s);
249                 case NOMENCL_PRINT_CODE:
250                         return InsetPrintNomencl::isCompatibleCommand(s);
251                 case REF_CODE:
252                         return InsetRef::isCompatibleCommand(s);
253                 case TOC_CODE:
254                         return InsetTOC::isCompatibleCommand(s);
255         default:
256                 LATTEST(false);
257                 // fall through in release mode
258         }
259         return false;
260 }
261
262
263 void InsetCommandParams::setCmdName(string const & name)
264 {
265         if (!isCompatibleCommand(insetCode_, name)) {
266                 LYXERR0("InsetCommand: Incompatible command name " << 
267                                 name << ".");
268                 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
269                                        _("Incompatible command name."));
270         }
271
272         cmdName_ = name;
273         info_ = findInfo(insetCode_, cmdName_);
274 }
275
276
277 void InsetCommandParams::read(Lexer & lex)
278 {
279         lex.setContext("InsetCommandParams::read");
280         lex >> insetName(insetCode_).c_str();
281         lex >> "LatexCommand";
282         lex >> cmdName_;
283         if (!isCompatibleCommand(insetCode_, cmdName_)) {
284                 lex.printError("Incompatible command name " + cmdName_ + ".");
285                 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
286                                        _("Incompatible command name."));
287         }
288
289         info_ = findInfo(insetCode_, cmdName_);
290         
291         string token;
292         while (lex.isOK()) {
293                 lex.next();
294                 token = lex.getString();
295                 if (token == "\\end_inset")
296                         break;
297                 if (token == "preview") {
298                         lex.next();
299                         preview_ = lex.getBool();
300                         continue;
301                 }
302                 if (info_.hasParam(token)) {
303                         lex.next(true);
304                         params_[token] = lex.getDocString();
305                 } else {
306                         lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
307                         throw ExceptionMessage(WarningException,
308                                 _("InsetCommandParams: ") + from_ascii(cmdName_),
309                                 _("Unknown parameter name: ") + from_utf8(token));
310                 }
311         }
312         if (token != "\\end_inset") {
313                 lex.printError("Missing \\end_inset at this point. "
314                                "Read: `$$Token'");
315                 throw ExceptionMessage(WarningException,
316                         _("InsetCommandParams Error: "),
317                         _("Missing \\end_inset at this point: ") + from_utf8(token));
318         }
319 }
320
321
322 void InsetCommandParams::write(ostream & os) const
323 {
324         os << "CommandInset " << insetType() << '\n';
325         os << "LatexCommand " << cmdName_ << '\n';
326         if (preview_)
327                 os << "preview true\n";
328         ParamInfo::const_iterator it  = info_.begin();
329         ParamInfo::const_iterator end = info_.end();
330         for (; it != end; ++it) {
331                 std::string const & name = it->name();
332                 docstring const & data = (*this)[name];
333                 if (!data.empty()) {
334                         // FIXME UNICODE
335                         os << name << ' '
336                            << Lexer::quoteString(to_utf8(data))
337                            << '\n';
338                 }
339         }
340 }
341
342
343 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
344 {
345         LASSERT(ci->isOptional(), return false);
346
347         ++ci; // we want to start with the next one
348         ParamInfo::const_iterator end = info_.end();
349         for (; ci != end; ++ci) {
350                 switch (ci->type()) {
351                 case ParamInfo::LYX_INTERNAL:
352                         break;
353
354                 case ParamInfo::LATEX_REQUIRED:
355                         return false;
356
357                 case ParamInfo::LATEX_OPTIONAL: {
358                         std::string const & name = ci->name();
359                         docstring const & data = (*this)[name];
360                         if (!data.empty())
361                                 return true;
362                         break;
363                 }
364
365                 } //end switch
366         }
367         return false;
368 }
369
370
371 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
372                                              docstring const & command,
373                                              ParamInfo::ParamHandling handling) const
374 {
375         docstring result;
376         switch (handling) {
377         case ParamInfo::HANDLING_LATEXIFY: {
378                 pair<docstring, docstring> command_latexed =
379                         runparams.encoding->latexString(command, runparams.dryrun);
380                 result = command_latexed.first;
381                 if (!command_latexed.second.empty()) {
382                         // issue a warning about omitted characters
383                         // FIXME: should be passed to the error dialog
384                         frontend::Alert::warning(_("Uncodable characters"),
385                                 bformat(_("The following characters that are used in the inset %1$s are not\n"
386                                           "representable in the current encoding and therefore have been omitted:\n%2$s."),
387                                         from_utf8(insetType()), command_latexed.second));
388                 }
389                 break;
390         } 
391         case ParamInfo::HANDLING_ESCAPE:
392                 result = escape(command);
393                 break;
394         case ParamInfo::HANDLING_NONE:
395                 result = command;
396                 break;
397         } // switch
398
399         return result;
400 }
401
402
403 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
404 {
405         docstring s = '\\' + from_ascii(cmdName_);
406         bool noparam = true;
407         ParamInfo::const_iterator it  = info_.begin();
408         ParamInfo::const_iterator end = info_.end();
409         for (; it != end; ++it) {
410                 std::string const & name = it->name();
411                 switch (it->type()) {
412                 case ParamInfo::LYX_INTERNAL:
413                         break;
414
415                 case ParamInfo::LATEX_REQUIRED: {
416                         docstring const data =
417                                 prepareCommand(runparams, (*this)[name], it->handling());
418                         s += '{' + data + '}';
419                         noparam = false;
420                         break;
421                 }
422                 case ParamInfo::LATEX_OPTIONAL: {
423                         docstring const data =
424                                 prepareCommand(runparams, (*this)[name], it->handling());
425                         if (!data.empty()) {
426                                 s += '[' + data + ']';
427                                 noparam = false;
428                         } else if (writeEmptyOptional(it)) {
429                                         s += "[]";
430                                         noparam = false;
431                         }
432                         break;
433                 } 
434                 } //end switch
435         }
436         if (noparam)
437                 // Make sure that following stuff does not change the
438                 // command name.
439                 s += "{}";
440         return s;
441 }
442
443
444 docstring InsetCommandParams::getFirstNonOptParam() const
445 {
446         ParamInfo::const_iterator it = 
447                 find_if(info_.begin(), info_.end(), 
448                         not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
449         LASSERT(it != info_.end(), return docstring());
450         return (*this)[it->name()];
451 }
452
453
454 docstring const & InsetCommandParams::operator[](string const & name) const
455 {
456         static const docstring dummy;
457         LASSERT(info_.hasParam(name), return dummy);
458         ParamMap::const_iterator data = params_.find(name);
459         if (data == params_.end() || data->second.empty())
460                 return dummy;
461         return data->second;
462 }
463
464
465 docstring & InsetCommandParams::operator[](string const & name)
466 {
467         LATTEST(info_.hasParam(name));
468         // this will add the name in release mode
469         return params_[name];
470 }
471
472
473 void InsetCommandParams::clear()
474 {
475         params_.clear();
476 }
477
478
479 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
480 {
481         return o1.insetCode_ == o2.insetCode_
482                 && o1.cmdName_ == o2.cmdName_
483                 && o1.info_ == o2.info_
484                 && o1.params_ == o2.params_
485                 && o1.preview_ == o2.preview_;
486 }
487
488
489 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)
490 {
491         return !(o1 == o2);
492 }
493
494
495 } // namespace lyx