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/InsetCounter.h"
30 #include "insets/InsetFlex.h"
31 #include "insets/InsetERT.h"
32 #include "insets/InsetListings.h"
33 #include "insets/InsetExternal.h"
34 #include "insets/InsetFloat.h"
35 #include "insets/InsetFloatList.h"
36 #include "insets/InsetFoot.h"
37 #include "insets/InsetGraphics.h"
38 #include "insets/InsetHyperlink.h"
39 #include "insets/InsetInclude.h"
40 #include "insets/InsetIndex.h"
41 #include "insets/InsetInfo.h"
42 #include "insets/InsetIPA.h"
43 #include "insets/InsetIPAMacro.h"
44 #include "insets/InsetLabel.h"
45 #include "insets/InsetLine.h"
46 #include "insets/InsetMarginal.h"
47 #include "insets/InsetNewline.h"
48 #include "insets/InsetNewpage.h"
49 #include "insets/InsetNomencl.h"
50 #include "insets/InsetNote.h"
51 #include "insets/InsetArgument.h"
52 #include "insets/InsetPhantom.h"
53 #include "insets/InsetPreview.h"
54 #include "insets/InsetRef.h"
55 #include "insets/InsetScript.h"
56 #include "insets/InsetSeparator.h"
57 #include "insets/InsetSpace.h"
58 #include "insets/InsetTabular.h"
59 #include "insets/InsetTOC.h"
60 #include "insets/InsetVSpace.h"
61 #include "insets/InsetWrap.h"
63 #include "mathed/InsetMathMacroTemplate.h"
64 #include "mathed/InsetMathHull.h"
66 #include "frontends/alert.h"
68 #include "support/debug.h"
69 #include "support/ExceptionMessage.h"
70 #include "support/lassert.h"
71 #include "support/lstrings.h"
72 #include "support/unique_ptr.h"
78 using namespace lyx::support;
82 namespace Alert = frontend::Alert;
85 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
89 switch (cmd.action()) {
91 case LFUN_NEWPAGE_INSERT: {
92 string const name = cmd.getArg(0);
93 InsetNewpageParams inp;
94 if (name.empty() || name == "newpage")
95 inp.kind = InsetNewpageParams::NEWPAGE;
96 else if (name == "pagebreak")
97 inp.kind = InsetNewpageParams::PAGEBREAK;
98 else if (name == "clearpage")
99 inp.kind = InsetNewpageParams::CLEARPAGE;
100 else if (name == "cleardoublepage")
101 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
102 return new InsetNewpage(inp);
105 case LFUN_SEPARATOR_INSERT: {
106 string const name = cmd.getArg(0);
107 InsetSeparatorParams inp;
108 if (name.empty() || name == "plain")
109 inp.kind = InsetSeparatorParams::PLAIN;
110 else if (name == "parbreak")
111 inp.kind = InsetSeparatorParams::PARBREAK;
112 else if (name == "latexpar")
113 inp.kind = InsetSeparatorParams::LATEXPAR;
115 lyxerr << "Wrong argument for LyX function 'separator-insert'." << endl;
118 return new InsetSeparator(inp);
121 case LFUN_FLEX_INSERT: {
122 string s = cmd.getArg(0);
123 return new InsetFlex(buf, s);
126 case LFUN_NOTE_INSERT: {
127 string arg = cmd.getArg(0);
130 return new InsetNote(buf, arg);
133 case LFUN_BOX_INSERT: {
134 string arg = cmd.getArg(0);
137 return new InsetBox(buf, arg);
140 case LFUN_BRANCH_INSERT: {
141 docstring arg = cmd.argument();
143 arg = from_ascii("none");
144 return new InsetBranch(buf, InsetBranchParams(arg));
147 case LFUN_PHANTOM_INSERT: {
148 string arg = cmd.getArg(0);
151 return new InsetPhantom(buf, arg);
154 case LFUN_IPAMACRO_INSERT: {
155 string const arg1 = cmd.getArg(0);
156 string const arg2 = cmd.getArg(1);
157 if (arg1 != "deco") {
158 LYXERR0("LFUN_IPAMACRO_INSERT: wrong argument");
161 return new InsetIPADeco(buf, arg2);
164 case LFUN_ERT_INSERT:
165 return new InsetERT(buf);
167 case LFUN_LISTING_INSERT:
168 return new InsetListings(buf);
170 case LFUN_FOOTNOTE_INSERT:
171 return new InsetFoot(buf);
173 case LFUN_MARGINALNOTE_INSERT:
174 return new InsetMarginal(buf);
176 case LFUN_ARGUMENT_INSERT: {
177 string arg = cmd.getArg(0);
179 LYXERR0("argument-insert needs an argument!");
182 return new InsetArgument(buf, arg);
185 case LFUN_FLOAT_INSERT: {
186 string argument = to_utf8(cmd.argument());
187 if (!argument.empty()) {
188 if (!contains(argument, "sideways")) {
189 if (!contains(argument, "wide"))
190 argument += "\nwide false";
191 argument += "\nsideways false";
194 return new InsetFloat(buf, argument);
197 case LFUN_FLOAT_WIDE_INSERT: {
198 string argument = to_utf8(cmd.argument());
199 if (!argument.empty()) {
200 if (!contains(argument, "sideways")) {
201 if (!contains(argument, "wide"))
202 argument += "\nwide true";
203 argument += "\nsideways false";
206 InsetFloat * fl = new InsetFloat(buf, argument);
211 case LFUN_WRAP_INSERT: {
212 string const argument = to_utf8(cmd.argument());
213 if (argument == "figure" || argument == "table")
214 return new InsetWrap(buf, argument);
215 lyxerr << "Non-existent wrapfig type: " << argument << endl;
219 case LFUN_INDEX_INSERT: {
220 docstring arg = cmd.argument();
221 return new InsetIndex(buf, InsetIndexParams(arg));
224 case LFUN_IPA_INSERT:
225 return new InsetIPA(buf);
227 case LFUN_NOMENCL_INSERT: {
228 InsetCommandParams icp(NOMENCL_CODE);
229 icp["symbol"] = cmd.argument();
230 return new InsetNomencl(buf, icp);
233 case LFUN_TABULAR_INSERT: {
234 if (cmd.argument().empty())
236 istringstream ss(to_utf8(cmd.argument()));
243 return new InsetTabular(buf, r, c);
246 case LFUN_CAPTION_INSERT: {
247 string arg = cmd.getArg(0);
250 return new InsetCaption(buf, arg);
253 case LFUN_INDEX_PRINT: {
254 InsetCommandParams icp(INDEX_PRINT_CODE);
255 icp["type"] = cmd.argument();
256 return new InsetPrintIndex(buf, icp);
259 case LFUN_NOMENCL_PRINT: {
260 InsetCommandParams icp(NOMENCL_PRINT_CODE);
261 icp["set_width"] = from_ascii("auto");
262 return new InsetPrintNomencl(buf, icp);
265 case LFUN_INFO_INSERT: {
266 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 InsetCommandParams icp(code);
307 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
308 return new InsetCounter(buf, icp);
312 return new InsetERT(buf,
313 InsetERT::string2params(to_utf8(cmd.argument())));
316 case EXTERNAL_CODE: {
317 InsetExternalParams iep;
318 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
319 auto inset = make_unique<InsetExternal>(buf);
320 inset->setBuffer(*buf);
321 inset->setParams(iep);
322 return inset.release();
325 case GRAPHICS_CODE: {
326 InsetGraphicsParams igp;
327 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
328 auto inset = make_unique<InsetGraphics>(buf);
329 inset->setParams(igp);
330 return inset.release();
333 case HYPERLINK_CODE: {
334 InsetCommandParams icp(code);
335 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
336 return new InsetHyperlink(buf, icp);
340 InsetCommandParams icp(code);
341 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
342 return new InsetInclude(buf, icp);
346 docstring arg = cmd.argument();
347 return new InsetIndex(buf, InsetIndexParams(arg));
350 case INDEX_PRINT_CODE: {
351 InsetCommandParams icp(code);
352 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
353 return new InsetPrintIndex(buf, icp);
357 InsetCommandParams icp(code);
358 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
359 return new InsetLabel(buf, icp);
363 InsetCommandParams icp(code);
364 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
365 return new InsetLine(buf, icp);
368 case LISTINGS_CODE: {
369 InsetListingsParams par;
370 InsetListings::string2params(to_utf8(cmd.argument()), par);
371 return new InsetListings(buf, par);
375 InsetCommandParams icp(code);
376 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
377 return new InsetNomencl(buf, icp);
381 InsetCommandParams icp(code);
382 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
383 return new InsetRef(buf, icp);
387 InsetScriptParams isp;
388 InsetScript::string2params(to_utf8(cmd.argument()), isp);
389 return new InsetScript(buf, isp);
393 InsetSpaceParams isp;
394 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
395 return new InsetSpace(isp);
399 InsetCommandParams icp(code);
400 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
401 return new InsetTOC(buf, icp);
406 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
407 return new InsetVSpace(vspace);
411 return new InsetPreview(buf);
414 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
419 } //end LFUN_INSET_INSERT
421 case LFUN_SPACE_INSERT: {
422 string const name = cmd.getArg(0);
423 string const len = cmd.getArg(1);
425 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
428 InsetSpaceParams isp;
429 // The tests for isp.math might be disabled after a file format change
430 if (name == "normal")
431 isp.kind = InsetSpaceParams::NORMAL;
432 else if (name == "protected")
433 isp.kind = InsetSpaceParams::PROTECTED;
434 else if (name == "visible")
435 isp.kind = InsetSpaceParams::VISIBLE;
436 else if (name == "thin")
437 isp.kind = InsetSpaceParams::THIN;
438 else if (isp.math && name == "med")
439 isp.kind = InsetSpaceParams::MEDIUM;
440 else if (isp.math && name == "thick")
441 isp.kind = InsetSpaceParams::THICK;
442 else if (name == "quad")
443 isp.kind = InsetSpaceParams::QUAD;
444 else if (name == "qquad")
445 isp.kind = InsetSpaceParams::QQUAD;
446 else if (name == "enspace")
447 isp.kind = InsetSpaceParams::ENSPACE;
448 else if (name == "enskip")
449 isp.kind = InsetSpaceParams::ENSKIP;
450 else if (name == "negthinspace")
451 isp.kind = InsetSpaceParams::NEGTHIN;
452 else if (name == "negmedspace")
453 isp.kind = InsetSpaceParams::NEGMEDIUM;
454 else if (name == "negthickspace")
455 isp.kind = InsetSpaceParams::NEGTHICK;
456 else if (name == "hfill")
457 isp.kind = InsetSpaceParams::HFILL;
458 else if (name == "hfill*")
459 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
460 else if (name == "dotfill")
461 isp.kind = InsetSpaceParams::DOTFILL;
462 else if (name == "hrulefill")
463 isp.kind = InsetSpaceParams::HRULEFILL;
464 else if (name == "hspace") {
465 if (len.empty() || !isValidGlueLength(len)) {
466 lyxerr << "LyX function 'space-insert hspace' "
467 << "needs a valid length argument." << endl;
470 isp.kind = InsetSpaceParams::CUSTOM;
471 isp.length = GlueLength(len);
473 else if (name == "hspace*") {
474 if (len.empty() || !isValidGlueLength(len)) {
475 lyxerr << "LyX function 'space-insert hspace*' "
476 << "needs a valid length argument." << endl;
479 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
480 isp.length = GlueLength(len);
483 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
486 return new InsetSpace(isp);
493 } catch (ExceptionMessage const & message) {
494 if (message.type_ == ErrorException) {
495 // This should never happen!
496 Alert::error(message.title_, message.details_);
498 } else if (message.type_ == WarningException) {
499 Alert::warning(message.title_, message.details_);
508 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
510 Inset * inset = createInsetHelper(buf, cmd);
512 inset->setBuffer(*buf);
517 Inset * readInset(Lexer & lex, Buffer * buf)
520 if (lex.getString() != "\\begin_inset")
521 LYXERR0("Buffer::readInset: Consistency check failed.");
523 unique_ptr<Inset> inset;
528 // test the different insets
530 // FIXME It would be better if we did not have this branch and could
531 // just do one massive switch for all insets. But at present, it's
532 // easier to do it this way, and we can't do the massive switch until
533 // the conversion mentioned below. Note that if we do want to do a
534 // single switch, we need to remove this "CommandInset" line---or
535 // replace it with a single "InsetType" line that would be used in all
537 if (tmptok == "CommandInset") {
539 string const insetType = lex.getString();
540 lex.pushToken(insetType);
542 InsetCode const code = insetCode(insetType);
544 //FIXME If we do the one massive switch, we cannot do this here, since
545 //we do not know in advance that we're dealing with a command inset.
546 //Worst case, we could put it in each case below. Better, we could
547 //pass the lexer to the constructor and let the params be built there.
548 InsetCommandParams inscmd(code);
549 inscmd.Read(lex, buf);
553 inset.reset(new InsetBibitem(buf, inscmd));
556 inset.reset(new InsetBibtex(buf, inscmd));
559 inset.reset(new InsetCitation(buf, inscmd));
562 inset.reset(new InsetCounter(buf, inscmd));
565 inset.reset(new InsetHyperlink(buf, inscmd));
568 inset.reset(new InsetInclude(buf, inscmd));
570 case INDEX_PRINT_CODE:
571 inset.reset(new InsetPrintIndex(buf, inscmd));
574 inset.reset(new InsetLabel(buf, inscmd));
577 inset.reset(new InsetLine(buf, inscmd));
580 inset.reset(new InsetNomencl(buf, inscmd));
582 case NOMENCL_PRINT_CODE:
583 inset.reset(new InsetPrintNomencl(buf, inscmd));
586 if (inscmd["name"].empty() && inscmd["reference"].empty())
588 inset.reset(new InsetRef(buf, inscmd));
591 inset.reset(new InsetTOC(buf, inscmd));
595 lyxerr << "unknown CommandInset '" << insetType
597 while (lex.isOK() && lex.getString() != "\\end_inset")
601 inset->setBuffer(*buf);
603 // FIXME This branch should be made to use inset codes
604 // as the preceding branch does. Unfortunately, that
605 // will take some doing. It requires converting the
606 // representation of the insets in LyX files so that
607 // they use the inset names listed in Inset.cpp. Then,
608 // as above, the inset names can be translated to
609 // inset codes using insetCode(). And the insets'
610 // write() routines should use insetName() rather than
612 if (tmptok == "Quotes") {
613 inset.reset(new InsetQuotes(buf));
614 } else if (tmptok == "External") {
615 inset.reset(new InsetExternal(buf));
616 } else if (tmptok == "FormulaMacro") {
617 inset.reset(new InsetMathMacroTemplate(buf));
618 } else if (tmptok == "Formula") {
619 inset.reset(new InsetMathHull(buf));
620 } else if (tmptok == "Graphics") {
621 inset.reset(new InsetGraphics(buf));
622 } else if (tmptok == "Note") {
623 inset.reset(new InsetNote(buf, tmptok));
624 } else if (tmptok == "Box") {
625 inset.reset(new InsetBox(buf, tmptok));
626 } else if (tmptok == "Flex") {
628 string s = lex.getString();
629 inset.reset(new InsetFlex(buf, s));
630 } else if (tmptok == "Branch") {
631 inset.reset(new InsetBranch(buf, InsetBranchParams()));
632 } else if (tmptok == "Phantom") {
633 inset.reset(new InsetPhantom(buf, tmptok));
634 } else if (tmptok == "ERT") {
635 inset.reset(new InsetERT(buf));
636 } else if (tmptok == "listings") {
637 inset.reset(new InsetListings(buf));
638 } else if (tmptok == "script") {
639 inset.reset(new InsetScript(buf));
640 } else if (tmptok == "space") {
641 inset.reset(new InsetSpace);
642 } else if (tmptok == "Tabular") {
643 inset.reset(new InsetTabular(buf));
644 } else if (tmptok == "Text") {
645 inset.reset(new InsetText(buf));
646 } else if (tmptok == "VSpace") {
647 inset.reset(new InsetVSpace);
648 } else if (tmptok == "Foot") {
649 inset.reset(new InsetFoot(buf));
650 } else if (tmptok == "Marginal") {
651 inset.reset(new InsetMarginal(buf));
652 } else if (tmptok == "Newpage") {
653 inset.reset(new InsetNewpage);
654 } else if (tmptok == "Newline") {
655 inset.reset(new InsetNewline);
656 } else if (tmptok == "Separator") {
657 inset.reset(new InsetSeparator);
658 } else if (tmptok == "Argument") {
659 inset.reset(new InsetArgument(buf, tmptok));
660 } else if (tmptok == "Float") {
661 inset.reset(new InsetFloat(buf, string()));
662 } else if (tmptok == "Wrap") {
664 string tmptok2 = lex.getString();
665 inset.reset(new InsetWrap(buf, tmptok2));
666 } else if (tmptok == "Caption") {
668 string s = lex.getString();
669 inset.reset(new InsetCaption(buf, s));
670 } else if (tmptok == "Index") {
671 inset.reset(new InsetIndex(buf, InsetIndexParams()));
672 } else if (tmptok == "FloatList") {
673 inset.reset(new InsetFloatList(buf));
674 } else if (tmptok == "Info") {
675 inset.reset(new InsetInfo(buf));
676 } else if (tmptok == "IPA") {
677 inset.reset(new InsetIPA(buf));
678 } else if (tmptok == "IPADeco") {
679 inset.reset(new InsetIPADeco(buf, tmptok));
680 } else if (tmptok == "Preview") {
681 inset.reset(new InsetPreview(buf));
683 lyxerr << "unknown Inset type '" << tmptok
685 while (lex.isOK() && lex.getString() != "\\end_inset")
690 // Set the buffer reference for proper parsing of some insets
691 // (InsetCollapsible for example)
692 inset->setBuffer(*buf);
694 // Set again the buffer for insets that are created inside this inset
695 // (InsetMathHull for example).
696 inset->setBuffer(*buf);
698 return inset.release();