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"
17 #include "FloatList.h"
18 #include "FuncRequest.h"
21 #include "TextClass.h"
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetBox.h"
26 #include "insets/InsetBranch.h"
27 #include "insets/InsetCaption.h"
28 #include "insets/InsetCitation.h"
29 #include "insets/InsetFlex.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/InsetHyperlink.h"
38 #include "insets/InsetInclude.h"
39 #include "insets/InsetIndex.h"
40 #include "insets/InsetInfo.h"
41 #include "insets/InsetIPA.h"
42 #include "insets/InsetIPAMacro.h"
43 #include "insets/InsetLabel.h"
44 #include "insets/InsetLine.h"
45 #include "insets/InsetMarginal.h"
46 #include "insets/InsetNewline.h"
47 #include "insets/InsetNewpage.h"
48 #include "insets/InsetNomencl.h"
49 #include "insets/InsetNote.h"
50 #include "insets/InsetArgument.h"
51 #include "insets/InsetPhantom.h"
52 #include "insets/InsetPreview.h"
53 #include "insets/InsetRef.h"
54 #include "insets/InsetScript.h"
55 #include "insets/InsetSeparator.h"
56 #include "insets/InsetSpace.h"
57 #include "insets/InsetTabular.h"
58 #include "insets/InsetTOC.h"
59 #include "insets/InsetVSpace.h"
60 #include "insets/InsetWrap.h"
62 #include "mathed/InsetMathMacroTemplate.h"
63 #include "mathed/InsetMathHull.h"
65 #include "frontends/alert.h"
67 #include "support/debug.h"
68 #include "support/ExceptionMessage.h"
69 #include "support/lassert.h"
70 #include "support/lstrings.h"
71 #include "support/unique_ptr.h"
77 using namespace lyx::support;
81 namespace Alert = frontend::Alert;
84 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
88 switch (cmd.action()) {
90 case LFUN_NEWPAGE_INSERT: {
91 string const name = cmd.getArg(0);
92 InsetNewpageParams inp;
93 if (name.empty() || name == "newpage")
94 inp.kind = InsetNewpageParams::NEWPAGE;
95 else if (name == "pagebreak")
96 inp.kind = InsetNewpageParams::PAGEBREAK;
97 else if (name == "clearpage")
98 inp.kind = InsetNewpageParams::CLEARPAGE;
99 else if (name == "cleardoublepage")
100 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
101 return new InsetNewpage(inp);
104 case LFUN_SEPARATOR_INSERT: {
105 string const name = cmd.getArg(0);
106 InsetSeparatorParams inp;
107 if (name.empty() || name == "plain")
108 inp.kind = InsetSeparatorParams::PLAIN;
109 else if (name == "parbreak")
110 inp.kind = InsetSeparatorParams::PARBREAK;
111 else if (name == "latexpar")
112 inp.kind = InsetSeparatorParams::LATEXPAR;
114 lyxerr << "Wrong argument for LyX function 'separator-insert'." << endl;
117 return new InsetSeparator(inp);
120 case LFUN_FLEX_INSERT: {
121 string s = cmd.getArg(0);
122 return new InsetFlex(buf, s);
125 case LFUN_NOTE_INSERT: {
126 string arg = cmd.getArg(0);
129 return new InsetNote(buf, arg);
132 case LFUN_BOX_INSERT: {
133 string arg = cmd.getArg(0);
136 return new InsetBox(buf, arg);
139 case LFUN_BRANCH_INSERT: {
140 docstring arg = cmd.argument();
142 arg = from_ascii("none");
143 return new InsetBranch(buf, InsetBranchParams(arg));
146 case LFUN_PHANTOM_INSERT: {
147 string arg = cmd.getArg(0);
150 return new InsetPhantom(buf, arg);
153 case LFUN_IPAMACRO_INSERT: {
154 string const arg1 = cmd.getArg(0);
155 string const arg2 = cmd.getArg(1);
156 if (arg1 != "deco") {
157 LYXERR0("LFUN_IPAMACRO_INSERT: wrong argument");
160 return new InsetIPADeco(buf, arg2);
163 case LFUN_ERT_INSERT:
164 return new InsetERT(buf);
166 case LFUN_LISTING_INSERT:
167 return new InsetListings(buf);
169 case LFUN_FOOTNOTE_INSERT:
170 return new InsetFoot(buf);
172 case LFUN_MARGINALNOTE_INSERT:
173 return new InsetMarginal(buf);
175 case LFUN_ARGUMENT_INSERT: {
176 string arg = cmd.getArg(0);
178 LYXERR0("argument-insert needs an argument!");
181 return new InsetArgument(buf, arg);
184 case LFUN_FLOAT_INSERT: {
185 string argument = to_utf8(cmd.argument());
186 if (!argument.empty()) {
187 if (!contains(argument, "sideways")) {
188 if (!contains(argument, "wide"))
189 argument += "\nwide false";
190 argument += "\nsideways false";
193 return new InsetFloat(buf, argument);
196 case LFUN_FLOAT_WIDE_INSERT: {
197 string argument = to_utf8(cmd.argument());
198 if (!argument.empty()) {
199 if (!contains(argument, "sideways")) {
200 if (!contains(argument, "wide"))
201 argument += "\nwide true";
202 argument += "\nsideways false";
205 InsetFloat * fl = new InsetFloat(buf, argument);
210 case LFUN_WRAP_INSERT: {
211 string const argument = to_utf8(cmd.argument());
212 if (argument == "figure" || argument == "table")
213 return new InsetWrap(buf, argument);
214 lyxerr << "Non-existent wrapfig type: " << argument << endl;
218 case LFUN_INDEX_INSERT: {
219 docstring arg = cmd.argument();
220 return new InsetIndex(buf, InsetIndexParams(arg));
223 case LFUN_IPA_INSERT:
224 return new InsetIPA(buf);
226 case LFUN_NOMENCL_INSERT: {
227 InsetCommandParams icp(NOMENCL_CODE);
228 icp["symbol"] = cmd.argument();
229 return new InsetNomencl(buf, icp);
232 case LFUN_TABULAR_INSERT: {
233 if (cmd.argument().empty())
235 istringstream ss(to_utf8(cmd.argument()));
242 return new InsetTabular(buf, r, c);
245 case LFUN_CAPTION_INSERT: {
246 string arg = cmd.getArg(0);
249 return new InsetCaption(buf, arg);
252 case LFUN_INDEX_PRINT: {
253 InsetCommandParams icp(INDEX_PRINT_CODE);
254 icp["type"] = cmd.argument();
255 return new InsetPrintIndex(buf, icp);
258 case LFUN_NOMENCL_PRINT: {
259 InsetCommandParams icp(NOMENCL_PRINT_CODE);
260 icp["set_width"] = from_ascii("auto");
261 return new InsetPrintNomencl(buf, icp);
264 case LFUN_INFO_INSERT: {
265 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
269 case LFUN_PREVIEW_INSERT:
270 return new InsetPreview(buf);
272 case LFUN_SCRIPT_INSERT: {
273 InsetScriptParams isp;
274 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
275 return new InsetScript(buf, isp);
278 case LFUN_INSET_INSERT: {
279 string const name = cmd.getArg(0);
280 InsetCode code = insetCode(name);
283 lyxerr << "No such inset '" << name << "'.";
287 InsetCommandParams icp(code);
288 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
289 return new InsetBibitem(buf, icp);
293 InsetCommandParams icp(code);
294 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
295 return new InsetBibtex(buf, icp);
299 InsetCommandParams icp(code);
300 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
301 return new InsetCitation(buf, icp);
305 return new InsetERT(buf,
306 InsetERT::string2params(to_utf8(cmd.argument())));
309 case EXTERNAL_CODE: {
310 InsetExternalParams iep;
311 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
312 auto inset = make_unique<InsetExternal>(buf);
313 inset->setBuffer(*buf);
314 inset->setParams(iep);
315 return inset.release();
318 case GRAPHICS_CODE: {
319 InsetGraphicsParams igp;
320 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
321 auto inset = make_unique<InsetGraphics>(buf);
322 inset->setParams(igp);
323 return inset.release();
326 case HYPERLINK_CODE: {
327 InsetCommandParams icp(code);
328 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
329 return new InsetHyperlink(buf, icp);
333 InsetCommandParams icp(code);
334 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
335 return new InsetInclude(buf, icp);
339 docstring arg = cmd.argument();
340 return new InsetIndex(buf, InsetIndexParams(arg));
343 case INDEX_PRINT_CODE: {
344 InsetCommandParams icp(code);
345 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
346 return new InsetPrintIndex(buf, icp);
350 InsetCommandParams icp(code);
351 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
352 return new InsetLabel(buf, icp);
356 InsetCommandParams icp(code);
357 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
358 return new InsetLine(buf, icp);
361 case LISTINGS_CODE: {
362 InsetListingsParams par;
363 InsetListings::string2params(to_utf8(cmd.argument()), par);
364 return new InsetListings(buf, par);
368 InsetCommandParams icp(code);
369 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
370 return new InsetNomencl(buf, icp);
374 InsetCommandParams icp(code);
375 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
376 return new InsetRef(buf, icp);
380 InsetScriptParams isp;
381 InsetScript::string2params(to_utf8(cmd.argument()), isp);
382 return new InsetScript(buf, isp);
386 InsetSpaceParams isp;
387 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
388 return new InsetSpace(isp);
392 InsetCommandParams icp(code);
393 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
394 return new InsetTOC(buf, icp);
399 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
400 return new InsetVSpace(vspace);
404 return new InsetPreview(buf);
407 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
412 } //end LFUN_INSET_INSERT
414 case LFUN_SPACE_INSERT: {
415 string const name = cmd.getArg(0);
416 string const len = cmd.getArg(1);
418 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
421 InsetSpaceParams isp;
422 // The tests for isp.math might be disabled after a file format change
423 if (name == "normal")
424 isp.kind = InsetSpaceParams::NORMAL;
425 else if (name == "protected")
426 isp.kind = InsetSpaceParams::PROTECTED;
427 else if (name == "visible")
428 isp.kind = InsetSpaceParams::VISIBLE;
429 else if (name == "thin")
430 isp.kind = InsetSpaceParams::THIN;
431 else if (isp.math && name == "med")
432 isp.kind = InsetSpaceParams::MEDIUM;
433 else if (isp.math && name == "thick")
434 isp.kind = InsetSpaceParams::THICK;
435 else if (name == "quad")
436 isp.kind = InsetSpaceParams::QUAD;
437 else if (name == "qquad")
438 isp.kind = InsetSpaceParams::QQUAD;
439 else if (name == "enspace")
440 isp.kind = InsetSpaceParams::ENSPACE;
441 else if (name == "enskip")
442 isp.kind = InsetSpaceParams::ENSKIP;
443 else if (name == "negthinspace")
444 isp.kind = InsetSpaceParams::NEGTHIN;
445 else if (name == "negmedspace")
446 isp.kind = InsetSpaceParams::NEGMEDIUM;
447 else if (name == "negthickspace")
448 isp.kind = InsetSpaceParams::NEGTHICK;
449 else if (name == "hfill")
450 isp.kind = InsetSpaceParams::HFILL;
451 else if (name == "hfill*")
452 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
453 else if (name == "dotfill")
454 isp.kind = InsetSpaceParams::DOTFILL;
455 else if (name == "hrulefill")
456 isp.kind = InsetSpaceParams::HRULEFILL;
457 else if (name == "hspace") {
458 if (len.empty() || !isValidGlueLength(len)) {
459 lyxerr << "LyX function 'space-insert hspace' "
460 << "needs a valid length argument." << endl;
463 isp.kind = InsetSpaceParams::CUSTOM;
464 isp.length = GlueLength(len);
466 else if (name == "hspace*") {
467 if (len.empty() || !isValidGlueLength(len)) {
468 lyxerr << "LyX function 'space-insert hspace*' "
469 << "needs a valid length argument." << endl;
472 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
473 isp.length = GlueLength(len);
476 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
479 return new InsetSpace(isp);
487 } catch (ExceptionMessage const & message) {
488 if (message.type_ == ErrorException) {
489 // This should never happen!
490 Alert::error(message.title_, message.details_);
492 } else if (message.type_ == WarningException) {
493 Alert::warning(message.title_, message.details_);
502 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
504 Inset * inset = createInsetHelper(buf, cmd);
506 inset->setBuffer(*buf);
511 Inset * readInset(Lexer & lex, Buffer * buf)
514 if (lex.getString() != "\\begin_inset")
515 LYXERR0("Buffer::readInset: Consistency check failed.");
517 unique_ptr<Inset> inset;
522 // test the different insets
524 // FIXME It would be better if we did not have this branch and could
525 // just do one massive switch for all insets. But at present, it's
526 // easier to do it this way, and we can't do the massive switch until
527 // the conversion mentioned below. Note that if we do want to do a
528 // single switch, we need to remove this "CommandInset" line---or
529 // replace it with a single "InsetType" line that would be used in all
531 if (tmptok == "CommandInset") {
533 string const insetType = lex.getString();
534 lex.pushToken(insetType);
536 InsetCode const code = insetCode(insetType);
538 //FIXME If we do the one massive switch, we cannot do this here, since
539 //we do not know in advance that we're dealing with a command inset.
540 //Worst case, we could put it in each case below. Better, we could
541 //pass the lexer to the constructor and let the params be built there.
542 InsetCommandParams inscmd(code);
543 inscmd.Read(lex, buf);
547 inset.reset(new InsetBibitem(buf, inscmd));
550 inset.reset(new InsetBibtex(buf, inscmd));
553 inset.reset(new InsetCitation(buf, inscmd));
556 inset.reset(new InsetHyperlink(buf, inscmd));
559 inset.reset(new InsetInclude(buf, inscmd));
561 case INDEX_PRINT_CODE:
562 inset.reset(new InsetPrintIndex(buf, inscmd));
565 inset.reset(new InsetLabel(buf, inscmd));
568 inset.reset(new InsetLine(buf, inscmd));
571 inset.reset(new InsetNomencl(buf, inscmd));
573 case NOMENCL_PRINT_CODE:
574 inset.reset(new InsetPrintNomencl(buf, inscmd));
577 if (inscmd["name"].empty() && inscmd["reference"].empty())
579 inset.reset(new InsetRef(buf, inscmd));
582 inset.reset(new InsetTOC(buf, inscmd));
586 lyxerr << "unknown CommandInset '" << insetType
588 while (lex.isOK() && lex.getString() != "\\end_inset")
592 inset->setBuffer(*buf);
594 // FIXME This branch should be made to use inset codes
595 // as the preceding branch does. Unfortunately, that
596 // will take some doing. It requires converting the
597 // representation of the insets in LyX files so that
598 // they use the inset names listed in Inset.cpp. Then,
599 // as above, the inset names can be translated to
600 // inset codes using insetCode(). And the insets'
601 // write() routines should use insetName() rather than
603 if (tmptok == "Quotes") {
604 inset.reset(new InsetQuotes(buf));
605 } else if (tmptok == "External") {
606 inset.reset(new InsetExternal(buf));
607 } else if (tmptok == "FormulaMacro") {
608 inset.reset(new InsetMathMacroTemplate(buf));
609 } else if (tmptok == "Formula") {
610 inset.reset(new InsetMathHull(buf));
611 } else if (tmptok == "Graphics") {
612 inset.reset(new InsetGraphics(buf));
613 } else if (tmptok == "Note") {
614 inset.reset(new InsetNote(buf, tmptok));
615 } else if (tmptok == "Box") {
616 inset.reset(new InsetBox(buf, tmptok));
617 } else if (tmptok == "Flex") {
619 string s = lex.getString();
620 inset.reset(new InsetFlex(buf, s));
621 } else if (tmptok == "Branch") {
622 inset.reset(new InsetBranch(buf, InsetBranchParams()));
623 } else if (tmptok == "Phantom") {
624 inset.reset(new InsetPhantom(buf, tmptok));
625 } else if (tmptok == "ERT") {
626 inset.reset(new InsetERT(buf));
627 } else if (tmptok == "listings") {
628 inset.reset(new InsetListings(buf));
629 } else if (tmptok == "script") {
630 inset.reset(new InsetScript(buf));
631 } else if (tmptok == "space") {
632 inset.reset(new InsetSpace);
633 } else if (tmptok == "Tabular") {
634 inset.reset(new InsetTabular(buf));
635 } else if (tmptok == "Text") {
636 inset.reset(new InsetText(buf));
637 } else if (tmptok == "VSpace") {
638 inset.reset(new InsetVSpace);
639 } else if (tmptok == "Foot") {
640 inset.reset(new InsetFoot(buf));
641 } else if (tmptok == "Marginal") {
642 inset.reset(new InsetMarginal(buf));
643 } else if (tmptok == "Newpage") {
644 inset.reset(new InsetNewpage);
645 } else if (tmptok == "Newline") {
646 inset.reset(new InsetNewline);
647 } else if (tmptok == "Separator") {
648 inset.reset(new InsetSeparator);
649 } else if (tmptok == "Argument") {
650 inset.reset(new InsetArgument(buf, tmptok));
651 } else if (tmptok == "Float") {
652 inset.reset(new InsetFloat(buf, string()));
653 } else if (tmptok == "Wrap") {
655 string tmptok2 = lex.getString();
656 inset.reset(new InsetWrap(buf, tmptok2));
657 } else if (tmptok == "Caption") {
659 string s = lex.getString();
660 inset.reset(new InsetCaption(buf, s));
661 } else if (tmptok == "Index") {
662 inset.reset(new InsetIndex(buf, InsetIndexParams()));
663 } else if (tmptok == "FloatList") {
664 inset.reset(new InsetFloatList(buf));
665 } else if (tmptok == "Info") {
666 inset.reset(new InsetInfo(buf));
667 } else if (tmptok == "IPA") {
668 inset.reset(new InsetIPA(buf));
669 } else if (tmptok == "IPADeco") {
670 inset.reset(new InsetIPADeco(buf, tmptok));
671 } else if (tmptok == "Preview") {
672 inset.reset(new InsetPreview(buf));
674 lyxerr << "unknown Inset type '" << tmptok
676 while (lex.isOK() && lex.getString() != "\\end_inset")
681 // Set the buffer reference for proper parsing of some insets
682 // (InsetCollapsible for example)
683 inset->setBuffer(*buf);
685 // Set again the buffer for insets that are created inside this inset
686 // (InsetMathHull for example).
687 inset->setBuffer(*buf);
689 return inset.release();