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.
15 #include "FuncRequest.h"
19 #include "insets/InsetBibitem.h"
20 #include "insets/InsetBibtex.h"
21 #include "insets/InsetBox.h"
22 #include "insets/InsetBranch.h"
23 #include "insets/InsetCaption.h"
24 #include "insets/InsetCitation.h"
25 #include "insets/InsetCounter.h"
26 #include "insets/InsetFlex.h"
27 #include "insets/InsetERT.h"
28 #include "insets/InsetListings.h"
29 #include "insets/InsetExternal.h"
30 #include "insets/InsetFloat.h"
31 #include "insets/InsetFloatList.h"
32 #include "insets/InsetFoot.h"
33 #include "insets/InsetGraphics.h"
34 #include "insets/InsetHyperlink.h"
35 #include "insets/InsetInclude.h"
36 #include "insets/InsetIndex.h"
37 #include "insets/InsetInfo.h"
38 #include "insets/InsetIPA.h"
39 #include "insets/InsetIPAMacro.h"
40 #include "insets/InsetLabel.h"
41 #include "insets/InsetLine.h"
42 #include "insets/InsetMarginal.h"
43 #include "insets/InsetNewline.h"
44 #include "insets/InsetNewpage.h"
45 #include "insets/InsetNomencl.h"
46 #include "insets/InsetNote.h"
47 #include "insets/InsetArgument.h"
48 #include "insets/InsetPhantom.h"
49 #include "insets/InsetPreview.h"
50 #include "insets/InsetQuotes.h"
51 #include "insets/InsetRef.h"
52 #include "insets/InsetScript.h"
53 #include "insets/InsetSeparator.h"
54 #include "insets/InsetSpace.h"
55 #include "insets/InsetTabular.h"
56 #include "insets/InsetTOC.h"
57 #include "insets/InsetVSpace.h"
58 #include "insets/InsetWrap.h"
60 #include "mathed/InsetMathMacroTemplate.h"
61 #include "mathed/InsetMathHull.h"
63 #include "frontends/alert.h"
65 #include "support/debug.h"
66 #include "support/ExceptionMessage.h"
67 #include "support/lstrings.h"
68 #include "support/unique_ptr.h"
74 using namespace lyx::support;
78 namespace Alert = frontend::Alert;
81 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
85 switch (cmd.action()) {
87 case LFUN_NEWPAGE_INSERT: {
88 string const name = cmd.getArg(0);
89 InsetNewpageParams inp;
90 if (name.empty() || name == "newpage")
91 inp.kind = InsetNewpageParams::NEWPAGE;
92 else if (name == "pagebreak")
93 inp.kind = InsetNewpageParams::PAGEBREAK;
94 else if (name == "clearpage")
95 inp.kind = InsetNewpageParams::CLEARPAGE;
96 else if (name == "cleardoublepage")
97 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
98 else if (name == "nopagebreak")
99 inp.kind = InsetNewpageParams::NOPAGEBREAK;
100 return new InsetNewpage(inp);
103 case LFUN_SEPARATOR_INSERT: {
104 string const name = cmd.getArg(0);
105 InsetSeparatorParams inp;
106 if (name.empty() || name == "plain")
107 inp.kind = InsetSeparatorParams::PLAIN;
108 else if (name == "parbreak")
109 inp.kind = InsetSeparatorParams::PARBREAK;
110 else if (name == "latexpar")
111 inp.kind = InsetSeparatorParams::LATEXPAR;
113 lyxerr << "Wrong argument for LyX function 'separator-insert'." << endl;
116 return new InsetSeparator(inp);
119 case LFUN_FLEX_INSERT: {
120 string s = cmd.getArg(0);
121 return new InsetFlex(buf, s);
124 case LFUN_NOTE_INSERT: {
125 string arg = cmd.getArg(0);
128 return new InsetNote(buf, arg);
131 case LFUN_BOX_INSERT: {
132 string arg = cmd.getArg(0);
135 return new InsetBox(buf, arg);
138 case LFUN_BRANCH_INSERT: {
139 docstring arg = cmd.argument();
141 arg = from_ascii("none");
142 return new InsetBranch(buf, InsetBranchParams(arg));
145 case LFUN_PHANTOM_INSERT: {
146 string arg = cmd.getArg(0);
149 return new InsetPhantom(buf, arg);
152 case LFUN_IPAMACRO_INSERT: {
153 string const arg1 = cmd.getArg(0);
154 string const arg2 = cmd.getArg(1);
155 if (arg1 != "deco") {
156 LYXERR0("LFUN_IPAMACRO_INSERT: wrong argument");
159 return new InsetIPADeco(buf, arg2);
162 case LFUN_ERT_INSERT:
163 return new InsetERT(buf);
165 case LFUN_LISTING_INSERT:
166 return new InsetListings(buf);
168 case LFUN_FOOTNOTE_INSERT:
169 return new InsetFoot(buf);
171 case LFUN_MARGINALNOTE_INSERT:
172 return new InsetMarginal(buf);
174 case LFUN_ARGUMENT_INSERT: {
175 string arg = cmd.getArg(0);
177 LYXERR0("argument-insert needs an argument!");
180 return new InsetArgument(buf, arg);
183 case LFUN_FLOAT_INSERT: {
184 string argument = to_utf8(cmd.argument());
185 if (!argument.empty()) {
186 if (!contains(argument, "sideways")) {
187 if (!contains(argument, "wide"))
188 argument += "\nwide false";
189 argument += "\nsideways false";
192 return new InsetFloat(buf, argument);
195 case LFUN_FLOAT_WIDE_INSERT: {
196 string argument = to_utf8(cmd.argument());
197 if (!argument.empty()) {
198 if (!contains(argument, "sideways")) {
199 if (!contains(argument, "wide"))
200 argument += "\nwide true";
201 argument += "\nsideways false";
204 InsetFloat * fl = new InsetFloat(buf, argument);
209 case LFUN_WRAP_INSERT: {
210 string const argument = to_utf8(cmd.argument());
211 if (argument == "figure" || argument == "table")
212 return new InsetWrap(buf, argument);
213 lyxerr << "Non-existent wrapfig type: " << argument << endl;
217 case LFUN_INDEX_INSERT: {
218 docstring const & arg = cmd.argument();
219 return new InsetIndex(buf, InsetIndexParams(arg));
222 case LFUN_IPA_INSERT:
223 return new InsetIPA(buf);
225 case LFUN_NOMENCL_INSERT: {
226 InsetCommandParams icp(NOMENCL_CODE);
227 icp["symbol"] = cmd.argument();
228 return new InsetNomencl(buf, icp);
231 case LFUN_TABULAR_INSERT: {
232 if (cmd.argument().empty())
234 istringstream ss(to_utf8(cmd.argument()));
241 return new InsetTabular(buf, r, c);
244 case LFUN_CAPTION_INSERT: {
245 string arg = cmd.getArg(0);
248 return new InsetCaption(buf, arg);
251 case LFUN_INDEX_PRINT: {
252 InsetCommandParams icp(INDEX_PRINT_CODE);
253 icp["type"] = cmd.argument();
254 return new InsetPrintIndex(buf, icp);
257 case LFUN_NOMENCL_PRINT: {
258 InsetCommandParams icp(NOMENCL_PRINT_CODE);
259 icp["set_width"] = from_ascii("auto");
260 return new InsetPrintNomencl(buf, icp);
263 case LFUN_INFO_INSERT: {
264 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
268 case LFUN_PREVIEW_INSERT:
269 return new InsetPreview(buf);
271 case LFUN_SCRIPT_INSERT: {
272 InsetScriptParams isp;
273 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
274 return new InsetScript(buf, isp);
277 case LFUN_INSET_INSERT: {
278 string const name = cmd.getArg(0);
279 InsetCode code = insetCode(name);
282 lyxerr << "No such inset '" << name << "'.";
286 InsetCommandParams icp(code);
287 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
288 return new InsetBibitem(buf, icp);
292 InsetCommandParams icp(code);
293 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
294 return new InsetBibtex(buf, icp);
298 InsetCommandParams icp(code);
299 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
300 return new InsetCitation(buf, icp);
304 InsetCommandParams icp(code);
305 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
306 return new InsetCounter(buf, icp);
310 return new InsetERT(buf,
311 InsetERT::string2params(to_utf8(cmd.argument())));
314 case EXTERNAL_CODE: {
315 InsetExternalParams iep;
316 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
317 auto inset = make_unique<InsetExternal>(buf);
318 inset->setBuffer(*buf);
319 inset->setParams(iep);
320 return inset.release();
323 case GRAPHICS_CODE: {
324 InsetGraphicsParams igp;
325 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
326 auto inset = make_unique<InsetGraphics>(buf);
327 inset->setParams(igp);
328 return inset.release();
331 case HYPERLINK_CODE: {
332 InsetCommandParams icp(code);
333 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
334 return new InsetHyperlink(buf, icp);
338 InsetCommandParams icp(code);
339 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
340 return new InsetInclude(buf, icp);
344 docstring const & arg = cmd.argument();
345 return new InsetIndex(buf, InsetIndexParams(arg));
348 case INDEX_PRINT_CODE: {
349 InsetCommandParams icp(code);
350 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
351 return new InsetPrintIndex(buf, icp);
355 InsetCommandParams icp(code);
356 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
357 return new InsetLabel(buf, icp);
361 InsetCommandParams icp(code);
362 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
363 return new InsetLine(buf, icp);
366 case LISTINGS_CODE: {
367 InsetListingsParams par;
368 InsetListings::string2params(to_utf8(cmd.argument()), par);
369 return new InsetListings(buf, par);
373 InsetCommandParams icp(code);
374 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
375 return new InsetNomencl(buf, icp);
379 InsetCommandParams icp(code);
380 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
381 return new InsetRef(buf, icp);
385 InsetScriptParams isp;
386 InsetScript::string2params(to_utf8(cmd.argument()), isp);
387 return new InsetScript(buf, isp);
391 InsetSpaceParams isp;
392 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
393 return new InsetSpace(isp);
397 InsetCommandParams icp(code);
398 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
399 return new InsetTOC(buf, icp);
404 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
405 return new InsetVSpace(vspace);
409 return new InsetPreview(buf);
412 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
417 } //end LFUN_INSET_INSERT
419 case LFUN_SPACE_INSERT: {
420 string const name = cmd.getArg(0);
421 string const len = cmd.getArg(1);
423 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
426 InsetSpaceParams isp;
427 // The tests for isp.math might be disabled after a file format change
428 if (name == "normal")
429 isp.kind = InsetSpaceParams::NORMAL;
430 else if (name == "protected")
431 isp.kind = InsetSpaceParams::PROTECTED;
432 else if (name == "visible")
433 isp.kind = InsetSpaceParams::VISIBLE;
434 else if (name == "thin")
435 isp.kind = InsetSpaceParams::THIN;
436 else if (isp.math && name == "med")
437 isp.kind = InsetSpaceParams::MEDIUM;
438 else if (isp.math && name == "thick")
439 isp.kind = InsetSpaceParams::THICK;
440 else if (name == "quad")
441 isp.kind = InsetSpaceParams::QUAD;
442 else if (name == "qquad")
443 isp.kind = InsetSpaceParams::QQUAD;
444 else if (name == "enspace")
445 isp.kind = InsetSpaceParams::ENSPACE;
446 else if (name == "enskip")
447 isp.kind = InsetSpaceParams::ENSKIP;
448 else if (name == "negthinspace")
449 isp.kind = InsetSpaceParams::NEGTHIN;
450 else if (name == "negmedspace")
451 isp.kind = InsetSpaceParams::NEGMEDIUM;
452 else if (name == "negthickspace")
453 isp.kind = InsetSpaceParams::NEGTHICK;
454 else if (name == "hfill")
455 isp.kind = InsetSpaceParams::HFILL;
456 else if (name == "hfill*")
457 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
458 else if (name == "dotfill")
459 isp.kind = InsetSpaceParams::DOTFILL;
460 else if (name == "hrulefill")
461 isp.kind = InsetSpaceParams::HRULEFILL;
462 else if (name == "hspace") {
463 if (len.empty() || !isValidGlueLength(len)) {
464 lyxerr << "LyX function 'space-insert hspace' "
465 << "needs a valid length argument." << endl;
468 isp.kind = InsetSpaceParams::CUSTOM;
469 isp.length = GlueLength(len);
471 else if (name == "hspace*") {
472 if (len.empty() || !isValidGlueLength(len)) {
473 lyxerr << "LyX function 'space-insert hspace*' "
474 << "needs a valid length argument." << endl;
477 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
478 isp.length = GlueLength(len);
481 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
484 return new InsetSpace(isp);
491 } catch (ExceptionMessage const & message) {
492 if (message.type_ == ErrorException) {
493 // This should never happen!
494 Alert::error(message.title_, message.details_);
496 } else if (message.type_ == WarningException) {
497 Alert::warning(message.title_, message.details_);
506 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
508 Inset * inset = createInsetHelper(buf, cmd);
510 inset->setBuffer(*buf);
515 Inset * readInset(Lexer & lex, Buffer * buf)
518 if (lex.getString() != "\\begin_inset")
519 LYXERR0("Buffer::readInset: Consistency check failed.");
521 unique_ptr<Inset> inset;
526 // test the different insets
528 // FIXME It would be better if we did not have this branch and could
529 // just do one massive switch for all insets. But at present, it's
530 // easier to do it this way, and we can't do the massive switch until
531 // the conversion mentioned below. Note that if we do want to do a
532 // single switch, we need to remove this "CommandInset" line---or
533 // replace it with a single "InsetType" line that would be used in all
535 if (tmptok == "CommandInset") {
537 string const insetType = lex.getString();
538 lex.pushToken(insetType);
540 InsetCode const code = insetCode(insetType);
542 //FIXME If we do the one massive switch, we cannot do this here, since
543 //we do not know in advance that we're dealing with a command inset.
544 //Worst case, we could put it in each case below. Better, we could
545 //pass the lexer to the constructor and let the params be built there.
546 InsetCommandParams inscmd(code);
547 inscmd.Read(lex, buf);
551 inset.reset(new InsetBibitem(buf, inscmd));
554 inset.reset(new InsetBibtex(buf, inscmd));
557 inset.reset(new InsetCitation(buf, inscmd));
560 inset.reset(new InsetCounter(buf, inscmd));
563 inset.reset(new InsetHyperlink(buf, inscmd));
566 inset.reset(new InsetInclude(buf, inscmd));
568 case INDEX_PRINT_CODE:
569 inset.reset(new InsetPrintIndex(buf, inscmd));
572 inset.reset(new InsetLabel(buf, inscmd));
575 inset.reset(new InsetLine(buf, inscmd));
578 inset.reset(new InsetNomencl(buf, inscmd));
580 case NOMENCL_PRINT_CODE:
581 inset.reset(new InsetPrintNomencl(buf, inscmd));
584 if (inscmd["name"].empty() && inscmd["reference"].empty())
586 inset.reset(new InsetRef(buf, inscmd));
589 inset.reset(new InsetTOC(buf, inscmd));
593 lyxerr << "unknown CommandInset '" << insetType
595 while (lex.isOK() && lex.getString() != "\\end_inset")
599 inset->setBuffer(*buf);
601 // FIXME This branch should be made to use inset codes
602 // as the preceding branch does. Unfortunately, that
603 // will take some doing. It requires converting the
604 // representation of the insets in LyX files so that
605 // they use the inset names listed in Inset.cpp. Then,
606 // as above, the inset names can be translated to
607 // inset codes using insetCode(). And the insets'
608 // write() routines should use insetName() rather than
610 if (tmptok == "Quotes") {
611 inset.reset(new InsetQuotes(buf));
612 } else if (tmptok == "External") {
613 inset.reset(new InsetExternal(buf));
614 } else if (tmptok == "FormulaMacro") {
615 inset.reset(new InsetMathMacroTemplate(buf));
616 } else if (tmptok == "Formula") {
617 inset.reset(new InsetMathHull(buf));
618 } else if (tmptok == "Graphics") {
619 inset.reset(new InsetGraphics(buf));
620 } else if (tmptok == "Note") {
621 inset.reset(new InsetNote(buf, tmptok));
622 } else if (tmptok == "Box") {
623 inset.reset(new InsetBox(buf, tmptok));
624 } else if (tmptok == "Flex") {
626 string s = lex.getString();
627 inset.reset(new InsetFlex(buf, s));
628 } else if (tmptok == "Branch") {
629 inset.reset(new InsetBranch(buf, InsetBranchParams()));
630 } else if (tmptok == "Phantom") {
631 inset.reset(new InsetPhantom(buf, tmptok));
632 } else if (tmptok == "ERT") {
633 inset.reset(new InsetERT(buf));
634 } else if (tmptok == "listings") {
635 inset.reset(new InsetListings(buf));
636 } else if (tmptok == "script") {
637 inset.reset(new InsetScript(buf));
638 } else if (tmptok == "space") {
639 inset.reset(new InsetSpace);
640 } else if (tmptok == "Tabular") {
641 inset.reset(new InsetTabular(buf));
642 } else if (tmptok == "Text") {
643 inset.reset(new InsetText(buf));
644 } else if (tmptok == "VSpace") {
645 inset.reset(new InsetVSpace);
646 } else if (tmptok == "Foot") {
647 inset.reset(new InsetFoot(buf));
648 } else if (tmptok == "Marginal") {
649 inset.reset(new InsetMarginal(buf));
650 } else if (tmptok == "Newpage") {
651 inset.reset(new InsetNewpage);
652 } else if (tmptok == "Newline") {
653 inset.reset(new InsetNewline);
654 } else if (tmptok == "Separator") {
655 inset.reset(new InsetSeparator);
656 } else if (tmptok == "Argument") {
657 inset.reset(new InsetArgument(buf, tmptok));
658 } else if (tmptok == "Float") {
659 inset.reset(new InsetFloat(buf, string()));
660 } else if (tmptok == "Wrap") {
662 string tmptok2 = lex.getString();
663 inset.reset(new InsetWrap(buf, tmptok2));
664 } else if (tmptok == "Caption") {
666 string s = lex.getString();
667 inset.reset(new InsetCaption(buf, s));
668 } else if (tmptok == "Index") {
669 inset.reset(new InsetIndex(buf, InsetIndexParams()));
670 } else if (tmptok == "FloatList") {
671 inset.reset(new InsetFloatList(buf));
672 } else if (tmptok == "Info") {
673 inset.reset(new InsetInfo(buf));
674 } else if (tmptok == "IPA") {
675 inset.reset(new InsetIPA(buf));
676 } else if (tmptok == "IPADeco") {
677 inset.reset(new InsetIPADeco(buf, tmptok));
678 } else if (tmptok == "Preview") {
679 inset.reset(new InsetPreview(buf));
681 lyxerr << "unknown Inset type '" << tmptok
683 while (lex.isOK() && lex.getString() != "\\end_inset")
688 // Set the buffer reference for proper parsing of some insets
689 // (InsetCollapsible for example)
690 inset->setBuffer(*buf);
692 // Set again the buffer for insets that are created inside this inset
693 // (InsetMathHull for example).
694 inset->setBuffer(*buf);
696 return inset.release();