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/MathMacroTemplate.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()));
270 case LFUN_PREVIEW_INSERT:
271 return new InsetPreview(buf);
273 case LFUN_SCRIPT_INSERT: {
274 InsetScriptParams isp;
275 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
276 return new InsetScript(buf, isp);
279 case LFUN_INSET_INSERT: {
280 string const name = cmd.getArg(0);
281 InsetCode code = insetCode(name);
284 lyxerr << "No such inset '" << name << "'.";
288 InsetCommandParams icp(code);
289 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
290 return new InsetBibitem(buf, icp);
294 InsetCommandParams icp(code);
295 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
296 return new InsetBibtex(buf, icp);
300 InsetCommandParams icp(code);
301 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
302 return new InsetCitation(buf, icp);
306 return new InsetERT(buf,
307 InsetERT::string2params(to_utf8(cmd.argument())));
310 case EXTERNAL_CODE: {
311 InsetExternalParams iep;
312 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
313 auto inset = make_unique<InsetExternal>(buf);
314 inset->setBuffer(*buf);
315 inset->setParams(iep);
316 return inset.release();
319 case GRAPHICS_CODE: {
320 InsetGraphicsParams igp;
321 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
322 auto inset = make_unique<InsetGraphics>(buf);
323 inset->setParams(igp);
324 return inset.release();
327 case HYPERLINK_CODE: {
328 InsetCommandParams icp(code);
329 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
330 return new InsetHyperlink(buf, icp);
334 InsetCommandParams icp(code);
335 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
336 return new InsetInclude(buf, icp);
340 docstring arg = cmd.argument();
341 return new InsetIndex(buf, InsetIndexParams(arg));
344 case INDEX_PRINT_CODE: {
345 InsetCommandParams icp(code);
346 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
347 return new InsetPrintIndex(buf, icp);
351 InsetCommandParams icp(code);
352 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
353 return new InsetLabel(buf, icp);
357 InsetCommandParams icp(code);
358 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
359 return new InsetLine(buf, icp);
362 case LISTINGS_CODE: {
363 InsetListingsParams par;
364 InsetListings::string2params(to_utf8(cmd.argument()), par);
365 return new InsetListings(buf, par);
369 InsetCommandParams icp(code);
370 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
371 return new InsetNomencl(buf, icp);
375 InsetCommandParams icp(code);
376 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
377 return new InsetRef(buf, icp);
381 InsetScriptParams isp;
382 InsetScript::string2params(to_utf8(cmd.argument()), isp);
383 return new InsetScript(buf, isp);
387 InsetSpaceParams isp;
388 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
389 return new InsetSpace(isp);
393 InsetCommandParams icp(code);
394 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
395 return new InsetTOC(buf, icp);
400 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
401 return new InsetVSpace(vspace);
405 return new InsetPreview(buf);
408 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
413 } //end LFUN_INSET_INSERT
415 case LFUN_SPACE_INSERT: {
416 string const name = cmd.getArg(0);
417 string const len = cmd.getArg(1);
419 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
422 InsetSpaceParams isp;
423 // The tests for isp.math might be disabled after a file format change
424 if (name == "normal")
425 isp.kind = InsetSpaceParams::NORMAL;
426 else if (name == "protected")
427 isp.kind = InsetSpaceParams::PROTECTED;
428 else if (name == "visible")
429 isp.kind = InsetSpaceParams::VISIBLE;
430 else if (name == "thin")
431 isp.kind = InsetSpaceParams::THIN;
432 else if (isp.math && name == "med")
433 isp.kind = InsetSpaceParams::MEDIUM;
434 else if (isp.math && name == "thick")
435 isp.kind = InsetSpaceParams::THICK;
436 else if (name == "quad")
437 isp.kind = InsetSpaceParams::QUAD;
438 else if (name == "qquad")
439 isp.kind = InsetSpaceParams::QQUAD;
440 else if (name == "enspace")
441 isp.kind = InsetSpaceParams::ENSPACE;
442 else if (name == "enskip")
443 isp.kind = InsetSpaceParams::ENSKIP;
444 else if (name == "negthinspace")
445 isp.kind = InsetSpaceParams::NEGTHIN;
446 else if (name == "negmedspace")
447 isp.kind = InsetSpaceParams::NEGMEDIUM;
448 else if (name == "negthickspace")
449 isp.kind = InsetSpaceParams::NEGTHICK;
450 else if (name == "hfill")
451 isp.kind = InsetSpaceParams::HFILL;
452 else if (name == "hfill*")
453 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
454 else if (name == "dotfill")
455 isp.kind = InsetSpaceParams::DOTFILL;
456 else if (name == "hrulefill")
457 isp.kind = InsetSpaceParams::HRULEFILL;
458 else if (name == "hspace") {
459 if (len.empty() || !isValidGlueLength(len)) {
460 lyxerr << "LyX function 'space-insert hspace' "
461 << "needs a valid length argument." << endl;
464 isp.kind = InsetSpaceParams::CUSTOM;
465 isp.length = GlueLength(len);
467 else if (name == "hspace*") {
468 if (len.empty() || !isValidGlueLength(len)) {
469 lyxerr << "LyX function 'space-insert hspace*' "
470 << "needs a valid length argument." << endl;
473 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
474 isp.length = GlueLength(len);
477 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
480 return new InsetSpace(isp);
488 } catch (ExceptionMessage const & message) {
489 if (message.type_ == ErrorException) {
490 // This should never happen!
491 Alert::error(message.title_, message.details_);
493 } else if (message.type_ == WarningException) {
494 Alert::warning(message.title_, message.details_);
503 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
505 Inset * inset = createInsetHelper(buf, cmd);
507 inset->setBuffer(*buf);
512 Inset * readInset(Lexer & lex, Buffer * buf)
515 if (lex.getString() != "\\begin_inset")
516 LYXERR0("Buffer::readInset: Consistency check failed.");
518 unique_ptr<Inset> inset;
523 // test the different insets
525 // FIXME It would be better if we did not have this branch and could
526 // just do one massive switch for all insets. But at present, it's
527 // easier to do it this way, and we can't do the massive switch until
528 // the conversion mentioned below. Note that if we do want to do a
529 // single switch, we need to remove this "CommandInset" line---or
530 // replace it with a single "InsetType" line that would be used in all
532 if (tmptok == "CommandInset") {
534 string const insetType = lex.getString();
535 lex.pushToken(insetType);
537 InsetCode const code = insetCode(insetType);
539 //FIXME If we do the one massive switch, we cannot do this here, since
540 //we do not know in advance that we're dealing with a command inset.
541 //Worst case, we could put it in each case below. Better, we could
542 //pass the lexer to the constructor and let the params be built there.
543 InsetCommandParams inscmd(code);
544 inscmd.Read(lex, buf);
548 inset.reset(new InsetBibitem(buf, inscmd));
551 inset.reset(new InsetBibtex(buf, inscmd));
554 inset.reset(new InsetCitation(buf, inscmd));
557 inset.reset(new InsetHyperlink(buf, inscmd));
560 inset.reset(new InsetInclude(buf, inscmd));
562 case INDEX_PRINT_CODE:
563 inset.reset(new InsetPrintIndex(buf, inscmd));
566 inset.reset(new InsetLabel(buf, inscmd));
569 inset.reset(new InsetLine(buf, inscmd));
572 inset.reset(new InsetNomencl(buf, inscmd));
574 case NOMENCL_PRINT_CODE:
575 inset.reset(new InsetPrintNomencl(buf, inscmd));
578 if (inscmd["name"].empty() && inscmd["reference"].empty())
580 inset.reset(new InsetRef(buf, inscmd));
583 inset.reset(new InsetTOC(buf, inscmd));
587 lyxerr << "unknown CommandInset '" << insetType
589 while (lex.isOK() && lex.getString() != "\\end_inset")
593 inset->setBuffer(*buf);
595 // FIXME This branch should be made to use inset codes
596 // as the preceding branch does. Unfortunately, that
597 // will take some doing. It requires converting the
598 // representation of the insets in LyX files so that
599 // they use the inset names listed in Inset.cpp. Then,
600 // as above, the inset names can be translated to
601 // inset codes using insetCode(). And the insets'
602 // write() routines should use insetName() rather than
604 if (tmptok == "Quotes") {
605 inset.reset(new InsetQuotes(buf));
606 } else if (tmptok == "External") {
607 inset.reset(new InsetExternal(buf));
608 } else if (tmptok == "FormulaMacro") {
609 inset.reset(new MathMacroTemplate(buf));
610 } else if (tmptok == "Formula") {
611 inset.reset(new InsetMathHull(buf));
612 } else if (tmptok == "Graphics") {
613 inset.reset(new InsetGraphics(buf));
614 } else if (tmptok == "Note") {
615 inset.reset(new InsetNote(buf, tmptok));
616 } else if (tmptok == "Box") {
617 inset.reset(new InsetBox(buf, tmptok));
618 } else if (tmptok == "Flex") {
620 string s = lex.getString();
621 inset.reset(new InsetFlex(buf, s));
622 } else if (tmptok == "Branch") {
623 inset.reset(new InsetBranch(buf, InsetBranchParams()));
624 } else if (tmptok == "Phantom") {
625 inset.reset(new InsetPhantom(buf, tmptok));
626 } else if (tmptok == "ERT") {
627 inset.reset(new InsetERT(buf));
628 } else if (tmptok == "listings") {
629 inset.reset(new InsetListings(buf));
630 } else if (tmptok == "script") {
631 inset.reset(new InsetScript(buf));
632 } else if (tmptok == "space") {
633 inset.reset(new InsetSpace);
634 } else if (tmptok == "Tabular") {
635 inset.reset(new InsetTabular(buf));
636 } else if (tmptok == "Text") {
637 inset.reset(new InsetText(buf));
638 } else if (tmptok == "VSpace") {
639 inset.reset(new InsetVSpace);
640 } else if (tmptok == "Foot") {
641 inset.reset(new InsetFoot(buf));
642 } else if (tmptok == "Marginal") {
643 inset.reset(new InsetMarginal(buf));
644 } else if (tmptok == "Newpage") {
645 inset.reset(new InsetNewpage);
646 } else if (tmptok == "Newline") {
647 inset.reset(new InsetNewline);
648 } else if (tmptok == "Separator") {
649 inset.reset(new InsetSeparator);
650 } else if (tmptok == "Argument") {
651 inset.reset(new InsetArgument(buf, tmptok));
652 } else if (tmptok == "Float") {
653 inset.reset(new InsetFloat(buf, string()));
654 } else if (tmptok == "Wrap") {
656 string tmptok = lex.getString();
657 inset.reset(new InsetWrap(buf, tmptok));
658 } else if (tmptok == "Caption") {
660 string s = lex.getString();
661 inset.reset(new InsetCaption(buf, s));
662 } else if (tmptok == "Index") {
663 inset.reset(new InsetIndex(buf, InsetIndexParams()));
664 } else if (tmptok == "FloatList") {
665 inset.reset(new InsetFloatList(buf));
666 } else if (tmptok == "Info") {
667 inset.reset(new InsetInfo(buf));
668 } else if (tmptok == "IPA") {
669 inset.reset(new InsetIPA(buf));
670 } else if (tmptok == "IPADeco") {
671 inset.reset(new InsetIPADeco(buf, tmptok));
672 } else if (tmptok == "Preview") {
673 inset.reset(new InsetPreview(buf));
675 lyxerr << "unknown Inset type '" << tmptok
677 while (lex.isOK() && lex.getString() != "\\end_inset")
682 // Set the buffer reference for proper parsing of some insets
683 // (InsetCollapsable for example)
684 inset->setBuffer(*buf);
686 // Set again the buffer for insets that are created inside this inset
687 // (InsetMathHull for example).
688 inset->setBuffer(*buf);
690 return inset.release();