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/lstrings.h"
69 #include "support/ExceptionMessage.h"
71 #include "support/lassert.h"
76 using namespace lyx::support;
80 namespace Alert = frontend::Alert;
83 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
87 switch (cmd.action()) {
89 case LFUN_NEWPAGE_INSERT: {
90 string const name = cmd.getArg(0);
91 InsetNewpageParams inp;
92 if (name.empty() || name == "newpage")
93 inp.kind = InsetNewpageParams::NEWPAGE;
94 else if (name == "pagebreak")
95 inp.kind = InsetNewpageParams::PAGEBREAK;
96 else if (name == "clearpage")
97 inp.kind = InsetNewpageParams::CLEARPAGE;
98 else if (name == "cleardoublepage")
99 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
100 return new InsetNewpage(inp);
103 case LFUN_SEPARATOR_INSERT: {
104 string const name = cmd.getArg(0);
105 InsetSeparatorParams inp;
106 if (name.empty() || name == "plain")
107 inp.kind = InsetSeparatorParams::PLAIN;
108 else if (name == "parbreak")
109 inp.kind = InsetSeparatorParams::PARBREAK;
111 lyxerr << "Wrong argument for LyX function 'separator-insert'." << endl;
114 return new InsetSeparator(inp);
117 case LFUN_FLEX_INSERT: {
118 string s = cmd.getArg(0);
119 return new InsetFlex(buf, s);
122 case LFUN_NOTE_INSERT: {
123 string arg = cmd.getArg(0);
126 return new InsetNote(buf, arg);
129 case LFUN_BOX_INSERT: {
130 string arg = cmd.getArg(0);
133 return new InsetBox(buf, arg);
136 case LFUN_BRANCH_INSERT: {
137 docstring arg = cmd.argument();
139 arg = from_ascii("none");
140 return new InsetBranch(buf, InsetBranchParams(arg));
143 case LFUN_PHANTOM_INSERT: {
144 string arg = cmd.getArg(0);
147 return new InsetPhantom(buf, arg);
150 case LFUN_IPAMACRO_INSERT: {
151 string const arg1 = cmd.getArg(0);
152 string const arg2 = cmd.getArg(1);
153 if (arg1 != "deco") {
154 LYXERR0("LFUN_IPAMACRO_INSERT: wrong argument");
157 return new InsetIPADeco(buf, arg2);
160 case LFUN_ERT_INSERT:
161 return new InsetERT(buf);
163 case LFUN_LISTING_INSERT:
164 return new InsetListings(buf);
166 case LFUN_FOOTNOTE_INSERT:
167 return new InsetFoot(buf);
169 case LFUN_MARGINALNOTE_INSERT:
170 return new InsetMarginal(buf);
172 case LFUN_ARGUMENT_INSERT: {
173 string arg = cmd.getArg(0);
175 LYXERR0("argument-insert needs an argument!");
178 return new InsetArgument(buf, arg);
181 case LFUN_FLOAT_INSERT: {
182 string argument = to_utf8(cmd.argument());
183 if (!argument.empty()) {
184 if (!contains(argument, "sideways")) {
185 if (!contains(argument, "wide"))
186 argument += "\nwide false";
187 argument += "\nsideways false";
190 return new InsetFloat(buf, argument);
193 case LFUN_FLOAT_WIDE_INSERT: {
194 string argument = to_utf8(cmd.argument());
195 if (!argument.empty()) {
196 if (!contains(argument, "sideways")) {
197 if (!contains(argument, "wide"))
198 argument += "\nwide true";
199 argument += "\nsideways false";
202 InsetFloat * fl = new InsetFloat(buf, argument);
207 case LFUN_WRAP_INSERT: {
208 string const argument = to_utf8(cmd.argument());
209 if (argument == "figure" || argument == "table")
210 return new InsetWrap(buf, argument);
211 lyxerr << "Non-existent wrapfig type: " << argument << endl;
215 case LFUN_INDEX_INSERT: {
216 docstring arg = cmd.argument();
217 return new InsetIndex(buf, InsetIndexParams(arg));
220 case LFUN_IPA_INSERT:
221 return new InsetIPA(buf);
223 case LFUN_NOMENCL_INSERT: {
224 InsetCommandParams icp(NOMENCL_CODE);
225 icp["symbol"] = cmd.argument();
226 return new InsetNomencl(buf, icp);
229 case LFUN_TABULAR_INSERT: {
230 if (cmd.argument().empty())
232 istringstream ss(to_utf8(cmd.argument()));
239 return new InsetTabular(buf, r, c);
242 case LFUN_CAPTION_INSERT: {
243 string arg = cmd.getArg(0);
246 return new InsetCaption(buf, arg);
249 case LFUN_INDEX_PRINT: {
250 InsetCommandParams icp(INDEX_PRINT_CODE);
251 icp["type"] = cmd.argument();
252 return new InsetPrintIndex(buf, icp);
255 case LFUN_NOMENCL_PRINT: {
256 InsetCommandParams icp(NOMENCL_PRINT_CODE);
257 icp["set_width"] = from_ascii("auto");
258 return new InsetPrintNomencl(buf, icp);
261 case LFUN_INFO_INSERT: {
262 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
267 case LFUN_PREVIEW_INSERT:
268 return new InsetPreview(buf);
270 case LFUN_SCRIPT_INSERT: {
271 InsetScriptParams isp;
272 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
273 return new InsetScript(buf, isp);
276 case LFUN_INSET_INSERT: {
277 string const name = cmd.getArg(0);
278 InsetCode code = insetCode(name);
281 lyxerr << "No such inset '" << name << "'.";
285 InsetCommandParams icp(code);
286 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
287 return new InsetBibitem(buf, icp);
291 InsetCommandParams icp(code);
292 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
293 return new InsetBibtex(buf, icp);
297 InsetCommandParams icp(code);
298 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
299 return new InsetCitation(buf, icp);
303 return new InsetERT(buf,
304 InsetERT::string2params(to_utf8(cmd.argument())));
307 case EXTERNAL_CODE: {
308 InsetExternalParams iep;
309 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
310 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
311 inset->setBuffer(*buf);
312 inset->setParams(iep);
313 return inset.release();
316 case GRAPHICS_CODE: {
317 InsetGraphicsParams igp;
318 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
319 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
320 inset->setParams(igp);
321 return inset.release();
324 case HYPERLINK_CODE: {
325 InsetCommandParams icp(code);
326 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
327 return new InsetHyperlink(buf, icp);
331 InsetCommandParams icp(code);
332 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
333 return new InsetInclude(buf, icp);
337 docstring arg = cmd.argument();
338 return new InsetIndex(buf, InsetIndexParams(arg));
341 case INDEX_PRINT_CODE: {
342 InsetCommandParams icp(code);
343 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
344 return new InsetPrintIndex(buf, icp);
348 InsetCommandParams icp(code);
349 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
350 return new InsetLabel(buf, icp);
354 InsetCommandParams icp(code);
355 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
356 return new InsetLine(buf, icp);
359 case LISTINGS_CODE: {
360 InsetListingsParams par;
361 InsetListings::string2params(to_utf8(cmd.argument()), par);
362 return new InsetListings(buf, par);
366 InsetCommandParams icp(code);
367 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
368 return new InsetNomencl(buf, icp);
372 InsetCommandParams icp(code);
373 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
374 return new InsetRef(buf, icp);
378 InsetScriptParams isp;
379 InsetScript::string2params(to_utf8(cmd.argument()), isp);
380 return new InsetScript(buf, isp);
384 InsetSpaceParams isp;
385 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
386 return new InsetSpace(isp);
390 InsetCommandParams icp(code);
391 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
392 return new InsetTOC(buf, icp);
397 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
398 return new InsetVSpace(vspace);
402 return new InsetPreview(buf);
405 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
410 } //end LFUN_INSET_INSERT
412 case LFUN_SPACE_INSERT: {
413 string const name = cmd.getArg(0);
414 string const len = cmd.getArg(1);
416 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
419 InsetSpaceParams isp;
420 // The tests for isp.math might be disabled after a file format change
421 if (name == "normal")
422 isp.kind = InsetSpaceParams::NORMAL;
423 else if (name == "protected")
424 isp.kind = InsetSpaceParams::PROTECTED;
425 else if (name == "visible")
426 isp.kind = InsetSpaceParams::VISIBLE;
427 else if (name == "thin")
428 isp.kind = InsetSpaceParams::THIN;
429 else if (isp.math && name == "med")
430 isp.kind = InsetSpaceParams::MEDIUM;
431 else if (isp.math && name == "thick")
432 isp.kind = InsetSpaceParams::THICK;
433 else if (name == "quad")
434 isp.kind = InsetSpaceParams::QUAD;
435 else if (name == "qquad")
436 isp.kind = InsetSpaceParams::QQUAD;
437 else if (name == "enspace")
438 isp.kind = InsetSpaceParams::ENSPACE;
439 else if (name == "enskip")
440 isp.kind = InsetSpaceParams::ENSKIP;
441 else if (name == "negthinspace")
442 isp.kind = InsetSpaceParams::NEGTHIN;
443 else if (name == "negmedspace")
444 isp.kind = InsetSpaceParams::NEGMEDIUM;
445 else if (name == "negthickspace")
446 isp.kind = InsetSpaceParams::NEGTHICK;
447 else if (name == "hfill")
448 isp.kind = InsetSpaceParams::HFILL;
449 else if (name == "hfill*")
450 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
451 else if (name == "dotfill")
452 isp.kind = InsetSpaceParams::DOTFILL;
453 else if (name == "hrulefill")
454 isp.kind = InsetSpaceParams::HRULEFILL;
455 else if (name == "hspace") {
456 if (len.empty() || !isValidGlueLength(len)) {
457 lyxerr << "LyX function 'space-insert hspace' "
458 << "needs a valid length argument." << endl;
461 isp.kind = InsetSpaceParams::CUSTOM;
462 isp.length = GlueLength(len);
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_PROTECTED;
471 isp.length = GlueLength(len);
474 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
477 return new InsetSpace(isp);
485 } catch (ExceptionMessage const & message) {
486 if (message.type_ == ErrorException) {
487 // This should never happen!
488 Alert::error(message.title_, message.details_);
490 } else if (message.type_ == WarningException) {
491 Alert::warning(message.title_, message.details_);
500 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
502 Inset * inset = createInsetHelper(buf, cmd);
504 inset->setBuffer(*buf);
509 Inset * readInset(Lexer & lex, Buffer * buf)
512 if (lex.getString() != "\\begin_inset")
513 LYXERR0("Buffer::readInset: Consistency check failed.");
515 auto_ptr<Inset> inset;
520 // test the different insets
522 // FIXME It would be better if we did not have this branch and could
523 // just do one massive switch for all insets. But at present, it's
524 // easier to do it this way, and we can't do the massive switch until
525 // the conversion mentioned below. Note that if we do want to do a
526 // single switch, we need to remove this "CommandInset" line---or
527 // replace it with a single "InsetType" line that would be used in all
529 if (tmptok == "CommandInset") {
531 string const insetType = lex.getString();
532 lex.pushToken(insetType);
534 InsetCode const code = insetCode(insetType);
536 //FIXME If we do the one massive switch, we cannot do this here, since
537 //we do not know in advance that we're dealing with a command inset.
538 //Worst case, we could put it in each case below. Better, we could
539 //pass the lexer to the constructor and let the params be built there.
540 InsetCommandParams inscmd(code);
541 inscmd.Read(lex, buf);
545 inset.reset(new InsetBibitem(buf, inscmd));
548 inset.reset(new InsetBibtex(buf, inscmd));
551 inset.reset(new InsetCitation(buf, inscmd));
554 inset.reset(new InsetHyperlink(buf, inscmd));
557 inset.reset(new InsetInclude(buf, inscmd));
559 case INDEX_PRINT_CODE:
560 inset.reset(new InsetPrintIndex(buf, inscmd));
563 inset.reset(new InsetLabel(buf, inscmd));
566 inset.reset(new InsetLine(buf, inscmd));
569 inset.reset(new InsetNomencl(buf, inscmd));
571 case NOMENCL_PRINT_CODE:
572 inset.reset(new InsetPrintNomencl(buf, inscmd));
575 if (inscmd["name"].empty() && inscmd["reference"].empty())
577 inset.reset(new InsetRef(buf, inscmd));
580 inset.reset(new InsetTOC(buf, inscmd));
584 lyxerr << "unknown CommandInset '" << insetType
586 while (lex.isOK() && lex.getString() != "\\end_inset")
590 inset->setBuffer(*buf);
592 // FIXME This branch should be made to use inset codes
593 // as the preceding branch does. Unfortunately, that
594 // will take some doing. It requires converting the
595 // representation of the insets in LyX files so that
596 // they use the inset names listed in Inset.cpp. Then,
597 // as above, the inset names can be translated to
598 // inset codes using insetCode(). And the insets'
599 // write() routines should use insetName() rather than
601 if (tmptok == "Quotes") {
602 inset.reset(new InsetQuotes(buf));
603 } else if (tmptok == "External") {
604 inset.reset(new InsetExternal(buf));
605 } else if (tmptok == "FormulaMacro") {
606 inset.reset(new MathMacroTemplate(buf));
607 } else if (tmptok == "Formula") {
608 inset.reset(new InsetMathHull(buf));
609 } else if (tmptok == "Graphics") {
610 inset.reset(new InsetGraphics(buf));
611 } else if (tmptok == "Note") {
612 inset.reset(new InsetNote(buf, tmptok));
613 } else if (tmptok == "Box") {
614 inset.reset(new InsetBox(buf, tmptok));
615 } else if (tmptok == "Flex") {
617 string s = lex.getString();
618 inset.reset(new InsetFlex(buf, s));
619 } else if (tmptok == "Branch") {
620 inset.reset(new InsetBranch(buf, InsetBranchParams()));
621 } else if (tmptok == "Phantom") {
622 inset.reset(new InsetPhantom(buf, tmptok));
623 } else if (tmptok == "ERT") {
624 inset.reset(new InsetERT(buf));
625 } else if (tmptok == "listings") {
626 inset.reset(new InsetListings(buf));
627 } else if (tmptok == "script") {
628 inset.reset(new InsetScript(buf));
629 } else if (tmptok == "space") {
630 inset.reset(new InsetSpace);
631 } else if (tmptok == "Tabular") {
632 inset.reset(new InsetTabular(buf));
633 } else if (tmptok == "Text") {
634 inset.reset(new InsetText(buf));
635 } else if (tmptok == "VSpace") {
636 inset.reset(new InsetVSpace);
637 } else if (tmptok == "Foot") {
638 inset.reset(new InsetFoot(buf));
639 } else if (tmptok == "Marginal") {
640 inset.reset(new InsetMarginal(buf));
641 } else if (tmptok == "Newpage") {
642 inset.reset(new InsetNewpage);
643 } else if (tmptok == "Newline") {
644 inset.reset(new InsetNewline);
645 } else if (tmptok == "Separator") {
646 inset.reset(new InsetSeparator);
647 } else if (tmptok == "Argument") {
648 inset.reset(new InsetArgument(buf, tmptok));
649 } else if (tmptok == "Float") {
650 inset.reset(new InsetFloat(buf, string()));
651 } else if (tmptok == "Wrap") {
653 string tmptok = lex.getString();
654 inset.reset(new InsetWrap(buf, tmptok));
655 } else if (tmptok == "Caption") {
657 string s = lex.getString();
658 inset.reset(new InsetCaption(buf, s));
659 } else if (tmptok == "Index") {
660 inset.reset(new InsetIndex(buf, InsetIndexParams()));
661 } else if (tmptok == "FloatList") {
662 inset.reset(new InsetFloatList(buf));
663 } else if (tmptok == "Info") {
664 inset.reset(new InsetInfo(buf));
665 } else if (tmptok == "IPA") {
666 inset.reset(new InsetIPA(buf));
667 } else if (tmptok == "IPADeco") {
668 inset.reset(new InsetIPADeco(buf, tmptok));
669 } else if (tmptok == "Preview") {
670 inset.reset(new InsetPreview(buf));
672 lyxerr << "unknown Inset type '" << tmptok
674 while (lex.isOK() && lex.getString() != "\\end_inset")
679 // Set the buffer reference for proper parsing of some insets
680 // (InsetCollapsable for example)
681 inset->setBuffer(*buf);
683 // Set again the buffer for insets that are created inside this inset
684 // (InsetMathHull for example).
685 inset->setBuffer(*buf);
687 return inset.release();