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 else if (name == "nopagebreak")
103 inp.kind = InsetNewpageParams::NOPAGEBREAK;
104 return new InsetNewpage(inp);
107 case LFUN_SEPARATOR_INSERT: {
108 string const name = cmd.getArg(0);
109 InsetSeparatorParams inp;
110 if (name.empty() || name == "plain")
111 inp.kind = InsetSeparatorParams::PLAIN;
112 else if (name == "parbreak")
113 inp.kind = InsetSeparatorParams::PARBREAK;
114 else if (name == "latexpar")
115 inp.kind = InsetSeparatorParams::LATEXPAR;
117 lyxerr << "Wrong argument for LyX function 'separator-insert'." << endl;
120 return new InsetSeparator(inp);
123 case LFUN_FLEX_INSERT: {
124 string s = cmd.getArg(0);
125 return new InsetFlex(buf, s);
128 case LFUN_NOTE_INSERT: {
129 string arg = cmd.getArg(0);
132 return new InsetNote(buf, arg);
135 case LFUN_BOX_INSERT: {
136 string arg = cmd.getArg(0);
139 return new InsetBox(buf, arg);
142 case LFUN_BRANCH_INSERT: {
143 docstring arg = cmd.argument();
145 arg = from_ascii("none");
146 return new InsetBranch(buf, InsetBranchParams(arg));
149 case LFUN_PHANTOM_INSERT: {
150 string arg = cmd.getArg(0);
153 return new InsetPhantom(buf, arg);
156 case LFUN_IPAMACRO_INSERT: {
157 string const arg1 = cmd.getArg(0);
158 string const arg2 = cmd.getArg(1);
159 if (arg1 != "deco") {
160 LYXERR0("LFUN_IPAMACRO_INSERT: wrong argument");
163 return new InsetIPADeco(buf, arg2);
166 case LFUN_ERT_INSERT:
167 return new InsetERT(buf);
169 case LFUN_LISTING_INSERT:
170 return new InsetListings(buf);
172 case LFUN_FOOTNOTE_INSERT:
173 return new InsetFoot(buf);
175 case LFUN_MARGINALNOTE_INSERT:
176 return new InsetMarginal(buf);
178 case LFUN_ARGUMENT_INSERT: {
179 string arg = cmd.getArg(0);
181 LYXERR0("argument-insert needs an argument!");
184 return new InsetArgument(buf, arg);
187 case LFUN_FLOAT_INSERT: {
188 string argument = to_utf8(cmd.argument());
189 if (!argument.empty()) {
190 if (!contains(argument, "sideways")) {
191 if (!contains(argument, "wide"))
192 argument += "\nwide false";
193 argument += "\nsideways false";
196 return new InsetFloat(buf, argument);
199 case LFUN_FLOAT_WIDE_INSERT: {
200 string argument = to_utf8(cmd.argument());
201 if (!argument.empty()) {
202 if (!contains(argument, "sideways")) {
203 if (!contains(argument, "wide"))
204 argument += "\nwide true";
205 argument += "\nsideways false";
208 InsetFloat * fl = new InsetFloat(buf, argument);
213 case LFUN_WRAP_INSERT: {
214 string const argument = to_utf8(cmd.argument());
215 if (argument == "figure" || argument == "table")
216 return new InsetWrap(buf, argument);
217 lyxerr << "Non-existent wrapfig type: " << argument << endl;
221 case LFUN_INDEX_INSERT: {
222 docstring arg = cmd.argument();
223 return new InsetIndex(buf, InsetIndexParams(arg));
226 case LFUN_IPA_INSERT:
227 return new InsetIPA(buf);
229 case LFUN_NOMENCL_INSERT: {
230 InsetCommandParams icp(NOMENCL_CODE);
231 icp["symbol"] = cmd.argument();
232 return new InsetNomencl(buf, icp);
235 case LFUN_TABULAR_INSERT: {
236 if (cmd.argument().empty())
238 istringstream ss(to_utf8(cmd.argument()));
245 return new InsetTabular(buf, r, c);
248 case LFUN_CAPTION_INSERT: {
249 string arg = cmd.getArg(0);
252 return new InsetCaption(buf, arg);
255 case LFUN_INDEX_PRINT: {
256 InsetCommandParams icp(INDEX_PRINT_CODE);
257 icp["type"] = cmd.argument();
258 return new InsetPrintIndex(buf, icp);
261 case LFUN_NOMENCL_PRINT: {
262 InsetCommandParams icp(NOMENCL_PRINT_CODE);
263 icp["set_width"] = from_ascii("auto");
264 return new InsetPrintNomencl(buf, icp);
267 case LFUN_INFO_INSERT: {
268 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
272 case LFUN_PREVIEW_INSERT:
273 return new InsetPreview(buf);
275 case LFUN_SCRIPT_INSERT: {
276 InsetScriptParams isp;
277 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
278 return new InsetScript(buf, isp);
281 case LFUN_INSET_INSERT: {
282 string const name = cmd.getArg(0);
283 InsetCode code = insetCode(name);
286 lyxerr << "No such inset '" << name << "'.";
290 InsetCommandParams icp(code);
291 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
292 return new InsetBibitem(buf, icp);
296 InsetCommandParams icp(code);
297 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
298 return new InsetBibtex(buf, icp);
302 InsetCommandParams icp(code);
303 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
304 return new InsetCitation(buf, icp);
308 InsetCommandParams icp(code);
309 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
310 return new InsetCounter(buf, icp);
314 return new InsetERT(buf,
315 InsetERT::string2params(to_utf8(cmd.argument())));
318 case EXTERNAL_CODE: {
319 InsetExternalParams iep;
320 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
321 auto inset = make_unique<InsetExternal>(buf);
322 inset->setBuffer(*buf);
323 inset->setParams(iep);
324 return inset.release();
327 case GRAPHICS_CODE: {
328 InsetGraphicsParams igp;
329 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
330 auto inset = make_unique<InsetGraphics>(buf);
331 inset->setParams(igp);
332 return inset.release();
335 case HYPERLINK_CODE: {
336 InsetCommandParams icp(code);
337 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
338 return new InsetHyperlink(buf, icp);
342 InsetCommandParams icp(code);
343 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
344 return new InsetInclude(buf, icp);
348 docstring arg = cmd.argument();
349 return new InsetIndex(buf, InsetIndexParams(arg));
352 case INDEX_PRINT_CODE: {
353 InsetCommandParams icp(code);
354 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
355 return new InsetPrintIndex(buf, icp);
359 InsetCommandParams icp(code);
360 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
361 return new InsetLabel(buf, icp);
365 InsetCommandParams icp(code);
366 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
367 return new InsetLine(buf, icp);
370 case LISTINGS_CODE: {
371 InsetListingsParams par;
372 InsetListings::string2params(to_utf8(cmd.argument()), par);
373 return new InsetListings(buf, par);
377 InsetCommandParams icp(code);
378 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
379 return new InsetNomencl(buf, icp);
383 InsetCommandParams icp(code);
384 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
385 return new InsetRef(buf, icp);
389 InsetScriptParams isp;
390 InsetScript::string2params(to_utf8(cmd.argument()), isp);
391 return new InsetScript(buf, isp);
395 InsetSpaceParams isp;
396 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
397 return new InsetSpace(isp);
401 InsetCommandParams icp(code);
402 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
403 return new InsetTOC(buf, icp);
408 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
409 return new InsetVSpace(vspace);
413 return new InsetPreview(buf);
416 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
421 } //end LFUN_INSET_INSERT
423 case LFUN_SPACE_INSERT: {
424 string const name = cmd.getArg(0);
425 string const len = cmd.getArg(1);
427 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
430 InsetSpaceParams isp;
431 // The tests for isp.math might be disabled after a file format change
432 if (name == "normal")
433 isp.kind = InsetSpaceParams::NORMAL;
434 else if (name == "protected")
435 isp.kind = InsetSpaceParams::PROTECTED;
436 else if (name == "visible")
437 isp.kind = InsetSpaceParams::VISIBLE;
438 else if (name == "thin")
439 isp.kind = InsetSpaceParams::THIN;
440 else if (isp.math && name == "med")
441 isp.kind = InsetSpaceParams::MEDIUM;
442 else if (isp.math && name == "thick")
443 isp.kind = InsetSpaceParams::THICK;
444 else if (name == "quad")
445 isp.kind = InsetSpaceParams::QUAD;
446 else if (name == "qquad")
447 isp.kind = InsetSpaceParams::QQUAD;
448 else if (name == "enspace")
449 isp.kind = InsetSpaceParams::ENSPACE;
450 else if (name == "enskip")
451 isp.kind = InsetSpaceParams::ENSKIP;
452 else if (name == "negthinspace")
453 isp.kind = InsetSpaceParams::NEGTHIN;
454 else if (name == "negmedspace")
455 isp.kind = InsetSpaceParams::NEGMEDIUM;
456 else if (name == "negthickspace")
457 isp.kind = InsetSpaceParams::NEGTHICK;
458 else if (name == "hfill")
459 isp.kind = InsetSpaceParams::HFILL;
460 else if (name == "hfill*")
461 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
462 else if (name == "dotfill")
463 isp.kind = InsetSpaceParams::DOTFILL;
464 else if (name == "hrulefill")
465 isp.kind = InsetSpaceParams::HRULEFILL;
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;
473 isp.length = GlueLength(len);
475 else if (name == "hspace*") {
476 if (len.empty() || !isValidGlueLength(len)) {
477 lyxerr << "LyX function 'space-insert hspace*' "
478 << "needs a valid length argument." << endl;
481 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
482 isp.length = GlueLength(len);
485 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
488 return new InsetSpace(isp);
495 } catch (ExceptionMessage const & message) {
496 if (message.type_ == ErrorException) {
497 // This should never happen!
498 Alert::error(message.title_, message.details_);
500 } else if (message.type_ == WarningException) {
501 Alert::warning(message.title_, message.details_);
510 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
512 Inset * inset = createInsetHelper(buf, cmd);
514 inset->setBuffer(*buf);
519 Inset * readInset(Lexer & lex, Buffer * buf)
522 if (lex.getString() != "\\begin_inset")
523 LYXERR0("Buffer::readInset: Consistency check failed.");
525 unique_ptr<Inset> inset;
530 // test the different insets
532 // FIXME It would be better if we did not have this branch and could
533 // just do one massive switch for all insets. But at present, it's
534 // easier to do it this way, and we can't do the massive switch until
535 // the conversion mentioned below. Note that if we do want to do a
536 // single switch, we need to remove this "CommandInset" line---or
537 // replace it with a single "InsetType" line that would be used in all
539 if (tmptok == "CommandInset") {
541 string const insetType = lex.getString();
542 lex.pushToken(insetType);
544 InsetCode const code = insetCode(insetType);
546 //FIXME If we do the one massive switch, we cannot do this here, since
547 //we do not know in advance that we're dealing with a command inset.
548 //Worst case, we could put it in each case below. Better, we could
549 //pass the lexer to the constructor and let the params be built there.
550 InsetCommandParams inscmd(code);
551 inscmd.Read(lex, buf);
555 inset.reset(new InsetBibitem(buf, inscmd));
558 inset.reset(new InsetBibtex(buf, inscmd));
561 inset.reset(new InsetCitation(buf, inscmd));
564 inset.reset(new InsetCounter(buf, inscmd));
567 inset.reset(new InsetHyperlink(buf, inscmd));
570 inset.reset(new InsetInclude(buf, inscmd));
572 case INDEX_PRINT_CODE:
573 inset.reset(new InsetPrintIndex(buf, inscmd));
576 inset.reset(new InsetLabel(buf, inscmd));
579 inset.reset(new InsetLine(buf, inscmd));
582 inset.reset(new InsetNomencl(buf, inscmd));
584 case NOMENCL_PRINT_CODE:
585 inset.reset(new InsetPrintNomencl(buf, inscmd));
588 if (inscmd["name"].empty() && inscmd["reference"].empty())
590 inset.reset(new InsetRef(buf, inscmd));
593 inset.reset(new InsetTOC(buf, inscmd));
597 lyxerr << "unknown CommandInset '" << insetType
599 while (lex.isOK() && lex.getString() != "\\end_inset")
603 inset->setBuffer(*buf);
605 // FIXME This branch should be made to use inset codes
606 // as the preceding branch does. Unfortunately, that
607 // will take some doing. It requires converting the
608 // representation of the insets in LyX files so that
609 // they use the inset names listed in Inset.cpp. Then,
610 // as above, the inset names can be translated to
611 // inset codes using insetCode(). And the insets'
612 // write() routines should use insetName() rather than
614 if (tmptok == "Quotes") {
615 inset.reset(new InsetQuotes(buf));
616 } else if (tmptok == "External") {
617 inset.reset(new InsetExternal(buf));
618 } else if (tmptok == "FormulaMacro") {
619 inset.reset(new InsetMathMacroTemplate(buf));
620 } else if (tmptok == "Formula") {
621 inset.reset(new InsetMathHull(buf));
622 } else if (tmptok == "Graphics") {
623 inset.reset(new InsetGraphics(buf));
624 } else if (tmptok == "Note") {
625 inset.reset(new InsetNote(buf, tmptok));
626 } else if (tmptok == "Box") {
627 inset.reset(new InsetBox(buf, tmptok));
628 } else if (tmptok == "Flex") {
630 string s = lex.getString();
631 inset.reset(new InsetFlex(buf, s));
632 } else if (tmptok == "Branch") {
633 inset.reset(new InsetBranch(buf, InsetBranchParams()));
634 } else if (tmptok == "Phantom") {
635 inset.reset(new InsetPhantom(buf, tmptok));
636 } else if (tmptok == "ERT") {
637 inset.reset(new InsetERT(buf));
638 } else if (tmptok == "listings") {
639 inset.reset(new InsetListings(buf));
640 } else if (tmptok == "script") {
641 inset.reset(new InsetScript(buf));
642 } else if (tmptok == "space") {
643 inset.reset(new InsetSpace);
644 } else if (tmptok == "Tabular") {
645 inset.reset(new InsetTabular(buf));
646 } else if (tmptok == "Text") {
647 inset.reset(new InsetText(buf));
648 } else if (tmptok == "VSpace") {
649 inset.reset(new InsetVSpace);
650 } else if (tmptok == "Foot") {
651 inset.reset(new InsetFoot(buf));
652 } else if (tmptok == "Marginal") {
653 inset.reset(new InsetMarginal(buf));
654 } else if (tmptok == "Newpage") {
655 inset.reset(new InsetNewpage);
656 } else if (tmptok == "Newline") {
657 inset.reset(new InsetNewline);
658 } else if (tmptok == "Separator") {
659 inset.reset(new InsetSeparator);
660 } else if (tmptok == "Argument") {
661 inset.reset(new InsetArgument(buf, tmptok));
662 } else if (tmptok == "Float") {
663 inset.reset(new InsetFloat(buf, string()));
664 } else if (tmptok == "Wrap") {
666 string tmptok2 = lex.getString();
667 inset.reset(new InsetWrap(buf, tmptok2));
668 } else if (tmptok == "Caption") {
670 string s = lex.getString();
671 inset.reset(new InsetCaption(buf, s));
672 } else if (tmptok == "Index") {
673 inset.reset(new InsetIndex(buf, InsetIndexParams()));
674 } else if (tmptok == "FloatList") {
675 inset.reset(new InsetFloatList(buf));
676 } else if (tmptok == "Info") {
677 inset.reset(new InsetInfo(buf));
678 } else if (tmptok == "IPA") {
679 inset.reset(new InsetIPA(buf));
680 } else if (tmptok == "IPADeco") {
681 inset.reset(new InsetIPADeco(buf, tmptok));
682 } else if (tmptok == "Preview") {
683 inset.reset(new InsetPreview(buf));
685 lyxerr << "unknown Inset type '" << tmptok
687 while (lex.isOK() && lex.getString() != "\\end_inset")
692 // Set the buffer reference for proper parsing of some insets
693 // (InsetCollapsible for example)
694 inset->setBuffer(*buf);
696 // Set again the buffer for insets that are created inside this inset
697 // (InsetMathHull for example).
698 inset->setBuffer(*buf);
700 return inset.release();