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/InsetIndexMacro.cpp"
38 #include "insets/InsetInfo.h"
39 #include "insets/InsetIPA.h"
40 #include "insets/InsetIPAMacro.h"
41 #include "insets/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.h"
44 #include "insets/InsetNewline.h"
45 #include "insets/InsetNewpage.h"
46 #include "insets/InsetNomencl.h"
47 #include "insets/InsetNote.h"
48 #include "insets/InsetArgument.h"
49 #include "insets/InsetPhantom.h"
50 #include "insets/InsetPreview.h"
51 #include "insets/InsetQuotes.h"
52 #include "insets/InsetRef.h"
53 #include "insets/InsetScript.h"
54 #include "insets/InsetSeparator.h"
55 #include "insets/InsetSpace.h"
56 #include "insets/InsetTabular.h"
57 #include "insets/InsetTOC.h"
58 #include "insets/InsetVSpace.h"
59 #include "insets/InsetWrap.h"
61 #include "mathed/InsetMathMacroTemplate.h"
62 #include "mathed/InsetMathHull.h"
64 #include "frontends/alert.h"
66 #include "support/debug.h"
67 #include "support/ExceptionMessage.h"
68 #include "support/lstrings.h"
69 #include "support/unique_ptr.h"
75 using namespace lyx::support;
79 namespace Alert = frontend::Alert;
82 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
86 switch (cmd.action()) {
88 case LFUN_NEWPAGE_INSERT: {
89 string const name = cmd.getArg(0);
90 InsetNewpageParams inp;
91 if (name.empty() || name == "newpage")
92 inp.kind = InsetNewpageParams::NEWPAGE;
93 else if (name == "pagebreak")
94 inp.kind = InsetNewpageParams::PAGEBREAK;
95 else if (name == "clearpage")
96 inp.kind = InsetNewpageParams::CLEARPAGE;
97 else if (name == "cleardoublepage")
98 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
99 else if (name == "nopagebreak")
100 inp.kind = InsetNewpageParams::NOPAGEBREAK;
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_INDEXMACRO_INSERT: {
164 string const arg = cmd.getArg(0);
165 if (arg != "see" && arg != "seealso"
166 && arg != "subentry" && arg != "sortkey") {
167 LYXERR0("LFUN_INDEXMACRO_INSERT: wrong argument");
170 return new InsetIndexMacro(buf, arg);
173 case LFUN_ERT_INSERT:
174 return new InsetERT(buf);
176 case LFUN_LISTING_INSERT:
177 return new InsetListings(buf);
179 case LFUN_FOOTNOTE_INSERT:
180 return new InsetFoot(buf);
182 case LFUN_MARGINALNOTE_INSERT:
183 return new InsetMarginal(buf);
185 case LFUN_ARGUMENT_INSERT: {
186 string arg = cmd.getArg(0);
188 LYXERR0("argument-insert needs an argument!");
191 return new InsetArgument(buf, arg);
194 case LFUN_FLOAT_INSERT: {
195 string argument = to_utf8(cmd.argument());
196 if (!argument.empty()) {
197 if (!contains(argument, "sideways")) {
198 if (!contains(argument, "wide"))
199 argument += "\nwide false";
200 argument += "\nsideways false";
203 return new InsetFloat(buf, argument);
206 case LFUN_FLOAT_WIDE_INSERT: {
207 string argument = to_utf8(cmd.argument());
208 if (!argument.empty()) {
209 if (!contains(argument, "sideways")) {
210 if (!contains(argument, "wide"))
211 argument += "\nwide true";
212 argument += "\nsideways false";
215 InsetFloat * fl = new InsetFloat(buf, argument);
220 case LFUN_WRAP_INSERT: {
221 string const argument = to_utf8(cmd.argument());
222 if (argument == "figure" || argument == "table")
223 return new InsetWrap(buf, argument);
224 lyxerr << "Non-existent wrapfig type: " << argument << endl;
228 case LFUN_INDEX_INSERT: {
229 docstring const & arg = cmd.argument();
230 return new InsetIndex(buf, InsetIndexParams(arg));
233 case LFUN_IPA_INSERT:
234 return new InsetIPA(buf);
236 case LFUN_NOMENCL_INSERT: {
237 InsetCommandParams icp(NOMENCL_CODE);
238 icp["symbol"] = cmd.argument();
239 return new InsetNomencl(buf, icp);
242 case LFUN_TABULAR_INSERT: {
243 if (cmd.argument().empty())
245 istringstream ss(to_utf8(cmd.argument()));
252 return new InsetTabular(buf, r, c);
255 case LFUN_CAPTION_INSERT: {
256 string arg = cmd.getArg(0);
259 return new InsetCaption(buf, arg);
262 case LFUN_INDEX_PRINT: {
263 InsetCommandParams icp(INDEX_PRINT_CODE);
264 icp["type"] = cmd.argument();
265 return new InsetPrintIndex(buf, icp);
268 case LFUN_NOMENCL_PRINT: {
269 InsetCommandParams icp(NOMENCL_PRINT_CODE);
270 icp["set_width"] = from_ascii("auto");
271 return new InsetPrintNomencl(buf, icp);
274 case LFUN_INFO_INSERT: {
275 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
279 case LFUN_PREVIEW_INSERT:
280 return new InsetPreview(buf);
282 case LFUN_SCRIPT_INSERT: {
283 InsetScriptParams isp;
284 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
285 return new InsetScript(buf, isp);
288 case LFUN_INSET_INSERT: {
289 string const name = cmd.getArg(0);
290 InsetCode code = insetCode(name);
293 lyxerr << "No such inset '" << name << "'.";
297 InsetCommandParams icp(code);
298 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
299 return new InsetBibitem(buf, icp);
303 InsetCommandParams icp(code);
304 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
305 return new InsetBibtex(buf, icp);
309 InsetCommandParams icp(code);
310 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
311 return new InsetCitation(buf, icp);
315 InsetCommandParams icp(code);
316 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
317 return new InsetCounter(buf, icp);
321 return new InsetERT(buf,
322 InsetERT::string2params(to_utf8(cmd.argument())));
325 case EXTERNAL_CODE: {
326 InsetExternalParams iep;
327 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
328 auto inset = make_unique<InsetExternal>(buf);
329 inset->setBuffer(*buf);
330 inset->setParams(iep);
331 return inset.release();
334 case GRAPHICS_CODE: {
335 InsetGraphicsParams igp;
336 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
337 auto inset = make_unique<InsetGraphics>(buf);
338 inset->setParams(igp);
339 return inset.release();
342 case HYPERLINK_CODE: {
343 InsetCommandParams icp(code);
344 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
345 return new InsetHyperlink(buf, icp);
349 InsetCommandParams icp(code);
350 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
351 return new InsetInclude(buf, icp);
355 docstring const & arg = cmd.argument();
356 return new InsetIndex(buf, InsetIndexParams(arg));
359 case INDEX_PRINT_CODE: {
360 InsetCommandParams icp(code);
361 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
362 return new InsetPrintIndex(buf, icp);
366 InsetCommandParams icp(code);
367 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
368 return new InsetLabel(buf, icp);
372 InsetCommandParams icp(code);
373 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
374 return new InsetLine(buf, icp);
377 case LISTINGS_CODE: {
378 InsetListingsParams par;
379 InsetListings::string2params(to_utf8(cmd.argument()), par);
380 return new InsetListings(buf, par);
384 InsetCommandParams icp(code);
385 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
386 return new InsetNomencl(buf, icp);
390 InsetCommandParams icp(code);
391 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
392 return new InsetRef(buf, icp);
396 InsetScriptParams isp;
397 InsetScript::string2params(to_utf8(cmd.argument()), isp);
398 return new InsetScript(buf, isp);
402 InsetSpaceParams isp;
403 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
404 return new InsetSpace(isp);
408 InsetCommandParams icp(code);
409 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
410 return new InsetTOC(buf, icp);
415 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
416 return new InsetVSpace(vspace);
420 return new InsetPreview(buf);
423 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
428 } //end LFUN_INSET_INSERT
430 case LFUN_SPACE_INSERT: {
431 string const name = cmd.getArg(0);
432 string const len = cmd.getArg(1);
434 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
437 InsetSpaceParams isp;
438 // The tests for isp.math might be disabled after a file format change
439 if (name == "normal")
440 isp.kind = InsetSpaceParams::NORMAL;
441 else if (name == "protected")
442 isp.kind = InsetSpaceParams::PROTECTED;
443 else if (name == "visible")
444 isp.kind = InsetSpaceParams::VISIBLE;
445 else if (name == "thin")
446 isp.kind = InsetSpaceParams::THIN;
447 else if (isp.math && name == "med")
448 isp.kind = InsetSpaceParams::MEDIUM;
449 else if (isp.math && name == "thick")
450 isp.kind = InsetSpaceParams::THICK;
451 else if (name == "quad")
452 isp.kind = InsetSpaceParams::QUAD;
453 else if (name == "qquad")
454 isp.kind = InsetSpaceParams::QQUAD;
455 else if (name == "enspace")
456 isp.kind = InsetSpaceParams::ENSPACE;
457 else if (name == "enskip")
458 isp.kind = InsetSpaceParams::ENSKIP;
459 else if (name == "negthinspace")
460 isp.kind = InsetSpaceParams::NEGTHIN;
461 else if (name == "negmedspace")
462 isp.kind = InsetSpaceParams::NEGMEDIUM;
463 else if (name == "negthickspace")
464 isp.kind = InsetSpaceParams::NEGTHICK;
465 else if (name == "hfill")
466 isp.kind = InsetSpaceParams::HFILL;
467 else if (name == "hfill*")
468 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
469 else if (name == "dotfill")
470 isp.kind = InsetSpaceParams::DOTFILL;
471 else if (name == "hrulefill")
472 isp.kind = InsetSpaceParams::HRULEFILL;
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;
480 isp.length = GlueLength(len);
482 else if (name == "hspace*") {
483 if (len.empty() || !isValidGlueLength(len)) {
484 lyxerr << "LyX function 'space-insert hspace*' "
485 << "needs a valid length argument." << endl;
488 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
489 isp.length = GlueLength(len);
492 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
495 return new InsetSpace(isp);
502 } catch (ExceptionMessage const & message) {
503 if (message.type_ == ErrorException) {
504 // This should never happen!
505 Alert::error(message.title_, message.details_);
507 } else if (message.type_ == WarningException) {
508 Alert::warning(message.title_, message.details_);
517 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
519 Inset * inset = createInsetHelper(buf, cmd);
521 inset->setBuffer(*buf);
526 Inset * readInset(Lexer & lex, Buffer * buf)
529 if (lex.getString() != "\\begin_inset")
530 LYXERR0("Buffer::readInset: Consistency check failed.");
532 unique_ptr<Inset> inset;
537 // test the different insets
539 // FIXME It would be better if we did not have this branch and could
540 // just do one massive switch for all insets. But at present, it's
541 // easier to do it this way, and we can't do the massive switch until
542 // the conversion mentioned below. Note that if we do want to do a
543 // single switch, we need to remove this "CommandInset" line---or
544 // replace it with a single "InsetType" line that would be used in all
546 if (tmptok == "CommandInset") {
548 string const insetType = lex.getString();
549 lex.pushToken(insetType);
551 InsetCode const code = insetCode(insetType);
553 //FIXME If we do the one massive switch, we cannot do this here, since
554 //we do not know in advance that we're dealing with a command inset.
555 //Worst case, we could put it in each case below. Better, we could
556 //pass the lexer to the constructor and let the params be built there.
557 InsetCommandParams inscmd(code);
558 inscmd.Read(lex, buf);
562 inset.reset(new InsetBibitem(buf, inscmd));
565 inset.reset(new InsetBibtex(buf, inscmd));
568 inset.reset(new InsetCitation(buf, inscmd));
571 inset.reset(new InsetCounter(buf, inscmd));
574 inset.reset(new InsetHyperlink(buf, inscmd));
577 inset.reset(new InsetInclude(buf, inscmd));
579 case INDEX_PRINT_CODE:
580 inset.reset(new InsetPrintIndex(buf, inscmd));
583 inset.reset(new InsetLabel(buf, inscmd));
586 inset.reset(new InsetLine(buf, inscmd));
589 inset.reset(new InsetNomencl(buf, inscmd));
591 case NOMENCL_PRINT_CODE:
592 inset.reset(new InsetPrintNomencl(buf, inscmd));
595 if (inscmd["name"].empty() && inscmd["reference"].empty())
597 inset.reset(new InsetRef(buf, inscmd));
600 inset.reset(new InsetTOC(buf, inscmd));
604 lyxerr << "unknown CommandInset '" << insetType
606 while (lex.isOK() && lex.getString() != "\\end_inset")
610 inset->setBuffer(*buf);
612 // FIXME This branch should be made to use inset codes
613 // as the preceding branch does. Unfortunately, that
614 // will take some doing. It requires converting the
615 // representation of the insets in LyX files so that
616 // they use the inset names listed in Inset.cpp. Then,
617 // as above, the inset names can be translated to
618 // inset codes using insetCode(). And the insets'
619 // write() routines should use insetName() rather than
621 if (tmptok == "Quotes") {
622 inset.reset(new InsetQuotes(buf));
623 } else if (tmptok == "External") {
624 inset.reset(new InsetExternal(buf));
625 } else if (tmptok == "FormulaMacro") {
626 inset.reset(new InsetMathMacroTemplate(buf));
627 } else if (tmptok == "Formula") {
628 inset.reset(new InsetMathHull(buf));
629 } else if (tmptok == "Graphics") {
630 inset.reset(new InsetGraphics(buf));
631 } else if (tmptok == "Note") {
632 inset.reset(new InsetNote(buf, tmptok));
633 } else if (tmptok == "Box") {
634 inset.reset(new InsetBox(buf, tmptok));
635 } else if (tmptok == "Flex") {
637 string s = lex.getString();
638 inset.reset(new InsetFlex(buf, s));
639 } else if (tmptok == "Branch") {
640 inset.reset(new InsetBranch(buf, InsetBranchParams()));
641 } else if (tmptok == "Phantom") {
642 inset.reset(new InsetPhantom(buf, tmptok));
643 } else if (tmptok == "ERT") {
644 inset.reset(new InsetERT(buf));
645 } else if (tmptok == "listings") {
646 inset.reset(new InsetListings(buf));
647 } else if (tmptok == "script") {
648 inset.reset(new InsetScript(buf));
649 } else if (tmptok == "space") {
650 inset.reset(new InsetSpace);
651 } else if (tmptok == "Tabular") {
652 inset.reset(new InsetTabular(buf));
653 } else if (tmptok == "Text") {
654 inset.reset(new InsetText(buf));
655 } else if (tmptok == "VSpace") {
656 inset.reset(new InsetVSpace);
657 } else if (tmptok == "Foot") {
658 inset.reset(new InsetFoot(buf));
659 } else if (tmptok == "Marginal") {
660 inset.reset(new InsetMarginal(buf));
661 } else if (tmptok == "Newpage") {
662 inset.reset(new InsetNewpage);
663 } else if (tmptok == "Newline") {
664 inset.reset(new InsetNewline);
665 } else if (tmptok == "Separator") {
666 inset.reset(new InsetSeparator);
667 } else if (tmptok == "Argument") {
668 inset.reset(new InsetArgument(buf, tmptok));
669 } else if (tmptok == "Float") {
670 inset.reset(new InsetFloat(buf, string()));
671 } else if (tmptok == "Wrap") {
673 string tmptok2 = lex.getString();
674 inset.reset(new InsetWrap(buf, tmptok2));
675 } else if (tmptok == "Caption") {
677 string s = lex.getString();
678 inset.reset(new InsetCaption(buf, s));
679 } else if (tmptok == "Index") {
680 inset.reset(new InsetIndex(buf, InsetIndexParams()));
681 } else if (tmptok == "IndexMacro") {
682 string s = lex.getString();
683 inset.reset(new InsetIndexMacro(buf, s));
684 } else if (tmptok == "FloatList") {
685 inset.reset(new InsetFloatList(buf));
686 } else if (tmptok == "Info") {
687 inset.reset(new InsetInfo(buf));
688 } else if (tmptok == "IPA") {
689 inset.reset(new InsetIPA(buf));
690 } else if (tmptok == "IPADeco") {
691 inset.reset(new InsetIPADeco(buf, tmptok));
692 } else if (tmptok == "Preview") {
693 inset.reset(new InsetPreview(buf));
695 lyxerr << "unknown Inset type '" << tmptok
697 while (lex.isOK() && lex.getString() != "\\end_inset")
702 // Set the buffer reference for proper parsing of some insets
703 // (InsetCollapsible for example)
704 inset->setBuffer(*buf);
706 // Set again the buffer for insets that are created inside this inset
707 // (InsetMathHull for example).
708 inset->setBuffer(*buf);
710 return inset.release();