3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
16 #include "BufferParams.h"
18 #include "FloatList.h"
19 #include "FuncRequest.h"
22 #include "TextClass.h"
24 #include "insets/InsetBibitem.h"
25 #include "insets/InsetBibtex.h"
26 #include "insets/InsetCaption.h"
27 #include "insets/InsetCitation.h"
28 #include "insets/InsetFlex.h"
29 #include "insets/InsetEnvironment.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetListings.h"
32 #include "insets/InsetExternal.h"
33 #include "insets/InsetFloat.h"
34 #include "insets/InsetFloatList.h"
35 #include "insets/InsetFoot.h"
36 #include "insets/InsetGraphics.h"
37 #include "insets/InsetHFill.h"
38 #include "insets/InsetInclude.h"
39 #include "insets/InsetIndex.h"
40 #include "insets/InsetInfo.h"
41 #include "insets/InsetNomencl.h"
42 #include "insets/InsetLabel.h"
43 #include "insets/InsetLine.h"
44 #include "insets/InsetMarginal.h"
45 #include "insets/InsetNote.h"
46 #include "insets/InsetBox.h"
47 #include "insets/InsetBranch.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetNewpage.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.h"
54 #include "insets/InsetHyperlink.h"
55 #include "insets/InsetVSpace.h"
56 #include "insets/InsetWrap.h"
58 #include "mathed/MathMacroTemplate.h"
59 #include "mathed/InsetMathHull.h"
61 #include "frontends/alert.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
66 #include <boost/assert.hpp>
67 #include <boost/current_function.hpp>
78 namespace Alert = frontend::Alert;
80 using support::compare_ascii_no_case;
83 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
85 BufferParams const & params = buf.params();
90 case LFUN_HFILL_INSERT:
91 return new InsetHFill;
93 case LFUN_LINE_INSERT:
96 case LFUN_NEWPAGE_INSERT:
97 return new InsetNewpage;
99 case LFUN_PAGEBREAK_INSERT:
100 return new InsetPagebreak;
102 case LFUN_CLEARPAGE_INSERT:
103 return new InsetClearPage;
105 case LFUN_CLEARDOUBLEPAGE_INSERT:
106 return new InsetClearDoublePage;
108 case LFUN_FLEX_INSERT: {
109 string s = cmd.getArg(0);
110 TextClass const & tclass = params.getTextClass();
111 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
112 return new InsetFlex(params, il);
115 case LFUN_NOTE_INSERT: {
116 string arg = cmd.getArg(0);
119 return new InsetNote(params, arg);
122 case LFUN_BOX_INSERT: {
123 string arg = cmd.getArg(0);
126 return new InsetBox(params, arg);
129 case LFUN_BRANCH_INSERT: {
130 docstring arg = cmd.argument();
132 arg = from_ascii("none");
133 return new InsetBranch(params, InsetBranchParams(arg));
136 case LFUN_ERT_INSERT:
137 return new InsetERT(params);
139 case LFUN_LISTING_INSERT:
140 return new InsetListings(params);
142 case LFUN_FOOTNOTE_INSERT:
143 return new InsetFoot(params);
145 case LFUN_MARGINALNOTE_INSERT:
146 return new InsetMarginal(params);
148 case LFUN_OPTIONAL_INSERT:
149 return new InsetOptArg(params);
151 case LFUN_BIBITEM_INSERT:
152 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
154 case LFUN_FLOAT_INSERT: {
155 // check if the float type exists
156 string const argument = to_utf8(cmd.argument());
157 if (params.getTextClass().floats().typeExist(argument))
158 return new InsetFloat(params, argument);
159 lyxerr << "Non-existent float type: " << argument << endl;
162 case LFUN_FLOAT_WIDE_INSERT: {
163 // check if the float type exists
164 string const argument = to_utf8(cmd.argument());
165 if (params.getTextClass().floats().typeExist(argument)) {
166 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
167 p->wide(true, params);
170 lyxerr << "Non-existent float type: " << argument << endl;
174 case LFUN_WRAP_INSERT: {
175 string const argument = to_utf8(cmd.argument());
176 if (argument == "figure" || argument == "table")
177 return new InsetWrap(params, argument);
178 lyxerr << "Non-existent wrapfig type: " << argument << endl;
182 case LFUN_INDEX_INSERT:
183 return new InsetIndex(params);
185 case LFUN_NOMENCL_INSERT: {
186 InsetCommandParams icp(NOMENCL_CODE);
187 icp["symbol"] = cmd.argument();
188 return new InsetNomencl(icp);
191 case LFUN_TABULAR_INSERT: {
192 if (cmd.argument().empty())
194 std::istringstream ss(to_utf8(cmd.argument()));
201 return new InsetTabular(buf, r, c);
204 case LFUN_CAPTION_INSERT: {
205 auto_ptr<InsetCaption> inset(new InsetCaption(params));
206 inset->setAutoBreakRows(true);
207 inset->setDrawFrame(true);
208 inset->setFrameColor(Color_captionframe);
209 return inset.release();
212 case LFUN_INDEX_PRINT:
213 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
215 case LFUN_NOMENCL_PRINT:
216 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
218 case LFUN_TOC_INSERT:
219 return new InsetTOC(InsetCommandParams(TOC_CODE));
221 case LFUN_ENVIRONMENT_INSERT:
222 return new InsetEnvironment(params, cmd.argument());
224 case LFUN_INFO_INSERT:
225 return new InsetInfo(params, to_utf8(cmd.argument()));
227 case LFUN_THEOREM_INSERT:
228 return new InsetTheorem;
231 case LFUN_INSET_INSERT: {
232 string const name = cmd.getArg(0);
233 InsetCode code = insetCode(name);
236 lyxerr << "No such inset '" << name << "'.";
240 InsetCommandParams icp(code);
241 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
242 return new InsetBibitem(icp);
246 InsetCommandParams icp(code);
247 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
248 return new InsetBibtex(icp);
252 InsetCommandParams icp(code);
253 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
254 return new InsetCitation(icp);
258 InsetCollapsable::CollapseStatus st;
259 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
260 return new InsetERT(params, st);
263 case LISTINGS_CODE: {
264 InsetListingsParams par;
265 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
266 return new InsetListings(params, par);
269 case EXTERNAL_CODE: {
270 InsetExternalParams iep;
271 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
272 auto_ptr<InsetExternal> inset(new InsetExternal);
273 inset->setParams(iep, buf);
274 return inset.release();
277 case GRAPHICS_CODE: {
278 InsetGraphicsParams igp;
279 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
280 auto_ptr<InsetGraphics> inset(new InsetGraphics);
281 inset->setParams(igp);
282 return inset.release();
285 case HYPERLINK_CODE: {
286 InsetCommandParams icp(code);
287 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
288 return new InsetHyperlink(icp);
292 InsetCommandParams icp(code);
293 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
294 return new InsetInclude(icp);
298 return new InsetIndex(params);
301 InsetCommandParams icp(code);
302 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
303 return new InsetNomencl(icp);
307 InsetCommandParams icp(code);
308 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
309 return new InsetLabel(icp);
313 InsetCommandParams icp(code);
314 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
315 return new InsetRef(icp, buf);
319 InsetCommandParams icp(code);
320 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
321 return new InsetTOC(icp);
326 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
327 return new InsetVSpace(vspace);
331 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
336 } //end LFUN_INSET_INSERT
338 case LFUN_SPACE_INSERT: {
339 string const name = to_utf8(cmd.argument());
340 if (name == "normal")
341 return new InsetSpace(InsetSpace::NORMAL);
342 if (name == "protected")
343 return new InsetSpace(InsetSpace::PROTECTED);
345 return new InsetSpace(InsetSpace::THIN);
347 return new InsetSpace(InsetSpace::QUAD);
349 return new InsetSpace(InsetSpace::QQUAD);
350 if (name == "enspace")
351 return new InsetSpace(InsetSpace::ENSPACE);
352 if (name == "enskip")
353 return new InsetSpace(InsetSpace::ENSKIP);
354 if (name == "negthinspace")
355 return new InsetSpace(InsetSpace::NEGTHIN);
357 lyxerr << "LyX function 'space' needs an argument." << endl;
359 lyxerr << "Wrong argument for LyX function 'space'." << endl;
367 } catch (support::ExceptionMessage const & message) {
368 if (message.type_ == support::ErrorException) {
369 Alert::error(message.title_, message.details_);
370 LyX::cref().emergencyCleanup();
372 } else if (message.type_ == support::WarningException) {
373 Alert::warning(message.title_, message.details_);
383 Inset * readInset(Lexer & lex, Buffer const & buf)
386 if (lex.getString() != "\\begin_inset") {
387 lyxerr << "Buffer::readInset: Consistency check failed."
391 auto_ptr<Inset> inset;
393 TextClass const & tclass = buf.params().getTextClass();
396 string tmptok = lex.getString();
398 // test the different insets
400 //FIXME It would be better if we did not have this branch and could
401 //just do one massive switch for all insets. But at present, it's easier
402 //to do it this way, and we can't do the massive switch until the conversion
404 //Note that if we do want to do a single switch, we need to remove
405 //this "CommandInset" line---or replace it with a single "InsetType" line
406 //that would be used in all insets.
407 if (tmptok == "CommandInset") {
409 string const insetType = lex.getString();
410 lex.pushToken(insetType);
412 InsetCode const code = insetCode(insetType);
414 //FIXME If we do the one massive switch, we cannot do this here, since
415 //we do not know in advance that we're dealing with a command inset.
416 //Worst case, we could put it in each case below. Better, we could
417 //pass the lexer to the constructor and let the params be built there.
418 InsetCommandParams inscmd(code);
423 inset.reset(new InsetBibitem(inscmd));
426 inset.reset(new InsetBibtex(inscmd));
429 inset.reset(new InsetCitation(inscmd));
432 inset.reset(new InsetHyperlink(inscmd));
435 inset.reset(new InsetInclude(inscmd));
437 case INDEX_PRINT_CODE:
438 inset.reset(new InsetPrintIndex(inscmd));
441 inset.reset(new InsetLabel(inscmd));
444 inset.reset(new InsetNomencl(inscmd));
446 case NOMENCL_PRINT_CODE:
447 inset.reset(new InsetPrintNomencl(inscmd));
450 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
451 inset.reset(new InsetRef(inscmd, buf));
454 inset.reset(new InsetTOC(inscmd));
458 lyxerr << "unknown CommandInset '" << insetType
460 while (lex.isOK() && lex.getString() != "\\end_inset")
465 // FIXME This branch should be made to use inset codes as the preceding
466 // branch does. Unfortunately, that will take some doing. It requires
467 // converting the representation of the insets in LyX files so that they
468 // use the inset names listed in Inset.cpp. Then, as above, the inset names
469 // can be translated to inset codes using insetCode(). And the insets'
470 // write() routines should use insetName() rather than hardcoding it.
471 if (tmptok == "Quotes") {
472 inset.reset(new InsetQuotes);
473 } else if (tmptok == "External") {
474 inset.reset(new InsetExternal);
475 } else if (tmptok == "FormulaMacro") {
476 inset.reset(new MathMacroTemplate);
477 } else if (tmptok == "Formula") {
478 inset.reset(new InsetMathHull);
479 } else if (tmptok == "Graphics") {
480 inset.reset(new InsetGraphics);
481 } else if (tmptok == "Note") {
482 inset.reset(new InsetNote(buf.params(), tmptok));
483 } else if (tmptok == "Box") {
484 inset.reset(new InsetBox(buf.params(), tmptok));
485 } else if (tmptok == "Flex") {
487 string s = lex.getString();
488 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
489 inset.reset(new InsetFlex(buf.params(), il));
490 } else if (tmptok == "Branch") {
491 inset.reset(new InsetBranch(buf.params(),
492 InsetBranchParams()));
493 } else if (tmptok == "Environment") {
495 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
496 } else if (tmptok == "ERT") {
497 inset.reset(new InsetERT(buf.params()));
498 } else if (tmptok == "listings") {
499 inset.reset(new InsetListings(buf.params()));
500 } else if (tmptok == "InsetSpace") {
501 inset.reset(new InsetSpace);
502 } else if (tmptok == "Tabular") {
503 inset.reset(new InsetTabular(buf));
504 } else if (tmptok == "Text") {
505 inset.reset(new InsetText(buf.params()));
506 } else if (tmptok == "VSpace") {
507 inset.reset(new InsetVSpace);
508 } else if (tmptok == "Foot") {
509 inset.reset(new InsetFoot(buf.params()));
510 } else if (tmptok == "Marginal") {
511 inset.reset(new InsetMarginal(buf.params()));
512 } else if (tmptok == "OptArg") {
513 inset.reset(new InsetOptArg(buf.params()));
514 } else if (tmptok == "Float") {
516 string tmptok = lex.getString();
517 inset.reset(new InsetFloat(buf.params(), tmptok));
518 } else if (tmptok == "Wrap") {
520 string tmptok = lex.getString();
521 inset.reset(new InsetWrap(buf.params(), tmptok));
523 } else if (tmptok == "Theorem") {
524 inset.reset(new InsetList);
526 } else if (tmptok == "Caption") {
527 inset.reset(new InsetCaption(buf.params()));
528 } else if (tmptok == "Index") {
529 inset.reset(new InsetIndex(buf.params()));
530 } else if (tmptok == "FloatList") {
531 inset.reset(new InsetFloatList);
532 } else if (tmptok == "Info") {
533 inset.reset(new InsetInfo(buf.params()));
535 lyxerr << "unknown Inset type '" << tmptok
537 while (lex.isOK() && lex.getString() != "\\end_inset")
542 inset->read(buf, lex);
545 return inset.release();