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/InsetLabel.h"
43 #include "insets/InsetLine.h"
44 #include "insets/InsetMarginal.h"
45 #include "insets/InsetNewline.h"
46 #include "insets/InsetNewpage.h"
47 #include "insets/InsetNomencl.h"
48 #include "insets/InsetNote.h"
49 #include "insets/InsetArgument.h"
50 #include "insets/InsetPhantom.h"
51 #include "insets/InsetPreview.h"
52 #include "insets/InsetRef.h"
53 #include "insets/InsetScript.h"
54 #include "insets/InsetSpace.h"
55 #include "insets/InsetTabular.h"
56 #include "insets/InsetTOC.h"
57 #include "insets/InsetVSpace.h"
58 #include "insets/InsetWrap.h"
60 #include "mathed/MathMacroTemplate.h"
61 #include "mathed/InsetMathHull.h"
63 #include "frontends/alert.h"
65 #include "support/debug.h"
66 #include "support/lstrings.h"
67 #include "support/ExceptionMessage.h"
69 #include "support/lassert.h"
74 using namespace lyx::support;
78 namespace Alert = frontend::Alert;
81 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
85 switch (cmd.action()) {
87 case LFUN_NEWPAGE_INSERT: {
88 string const name = cmd.getArg(0);
89 InsetNewpageParams inp;
90 if (name.empty() || name == "newpage")
91 inp.kind = InsetNewpageParams::NEWPAGE;
92 else if (name == "pagebreak")
93 inp.kind = InsetNewpageParams::PAGEBREAK;
94 else if (name == "clearpage")
95 inp.kind = InsetNewpageParams::CLEARPAGE;
96 else if (name == "cleardoublepage")
97 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
98 return new InsetNewpage(inp);
101 case LFUN_FLEX_INSERT: {
102 string s = cmd.getArg(0);
103 return new InsetFlex(buf, s);
106 case LFUN_NOTE_INSERT: {
107 string arg = cmd.getArg(0);
110 return new InsetNote(buf, arg);
113 case LFUN_BOX_INSERT: {
114 string arg = cmd.getArg(0);
117 return new InsetBox(buf, arg);
120 case LFUN_BRANCH_INSERT: {
121 docstring arg = cmd.argument();
123 arg = from_ascii("none");
124 return new InsetBranch(buf, InsetBranchParams(arg));
127 case LFUN_PHANTOM_INSERT: {
128 string arg = cmd.getArg(0);
131 return new InsetPhantom(buf, arg);
134 case LFUN_ERT_INSERT:
135 return new InsetERT(buf);
137 case LFUN_LISTING_INSERT:
138 return new InsetListings(buf);
140 case LFUN_FOOTNOTE_INSERT:
141 return new InsetFoot(buf);
143 case LFUN_MARGINALNOTE_INSERT:
144 return new InsetMarginal(buf);
146 case LFUN_ARGUMENT_INSERT:
147 return new InsetArgument(buf);
149 case LFUN_FLOAT_INSERT: {
150 string argument = to_utf8(cmd.argument());
151 if (!argument.empty()) {
152 if (!contains(argument, "sideways")) {
153 if (!contains(argument, "wide"))
154 argument += "\nwide false";
155 argument += "\nsideways false";
158 return new InsetFloat(buf, argument);
161 case LFUN_FLOAT_WIDE_INSERT: {
162 string argument = to_utf8(cmd.argument());
163 if (!argument.empty()) {
164 if (!contains(argument, "sideways")) {
165 if (!contains(argument, "wide"))
166 argument += "\nwide true";
167 argument += "\nsideways false";
170 InsetFloat * fl = new InsetFloat(buf, argument);
175 case LFUN_WRAP_INSERT: {
176 string const argument = to_utf8(cmd.argument());
177 if (argument == "figure" || argument == "table")
178 return new InsetWrap(buf, argument);
179 lyxerr << "Non-existent wrapfig type: " << argument << endl;
183 case LFUN_INDEX_INSERT: {
184 docstring arg = cmd.argument();
185 return new InsetIndex(buf, InsetIndexParams(arg));
188 case LFUN_IPA_INSERT:
189 return new InsetIPA(buf);
191 case LFUN_NOMENCL_INSERT: {
192 InsetCommandParams icp(NOMENCL_CODE);
193 icp["symbol"] = cmd.argument();
194 return new InsetNomencl(buf, icp);
197 case LFUN_TABULAR_INSERT: {
198 if (cmd.argument().empty())
200 istringstream ss(to_utf8(cmd.argument()));
207 return new InsetTabular(buf, r, c);
210 case LFUN_CAPTION_INSERT:
211 return new InsetCaption(buf);
213 case LFUN_INDEX_PRINT: {
214 InsetCommandParams icp(INDEX_PRINT_CODE);
215 icp["type"] = cmd.argument();
216 return new InsetPrintIndex(buf, icp);
219 case LFUN_NOMENCL_PRINT: {
220 InsetCommandParams icp(NOMENCL_PRINT_CODE);
221 icp["set_width"] = from_ascii("auto");
222 return new InsetPrintNomencl(buf, icp);
225 case LFUN_INFO_INSERT: {
226 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
231 case LFUN_PREVIEW_INSERT:
232 return new InsetPreview(buf);
234 case LFUN_SCRIPT_INSERT: {
235 InsetScriptParams isp;
236 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
237 return new InsetScript(buf, isp);
240 case LFUN_INSET_INSERT: {
241 string const name = cmd.getArg(0);
242 InsetCode code = insetCode(name);
245 lyxerr << "No such inset '" << name << "'.";
249 InsetCommandParams icp(code);
250 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
251 return new InsetBibitem(buf, icp);
255 InsetCommandParams icp(code);
256 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
257 return new InsetBibtex(buf, icp);
261 InsetCommandParams icp(code);
262 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
263 return new InsetCitation(buf, icp);
267 return new InsetERT(buf,
268 InsetERT::string2params(to_utf8(cmd.argument())));
271 case EXTERNAL_CODE: {
272 InsetExternalParams iep;
273 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
274 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
275 inset->setBuffer(*buf);
276 inset->setParams(iep);
277 return inset.release();
280 case GRAPHICS_CODE: {
281 InsetGraphicsParams igp;
282 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
283 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
284 inset->setParams(igp);
285 return inset.release();
288 case HYPERLINK_CODE: {
289 InsetCommandParams icp(code);
290 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
291 return new InsetHyperlink(buf, icp);
295 InsetCommandParams icp(code);
296 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
297 return new InsetInclude(buf, icp);
301 docstring arg = cmd.argument();
302 return new InsetIndex(buf, InsetIndexParams(arg));
305 case INDEX_PRINT_CODE: {
306 InsetCommandParams icp(code);
307 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
308 return new InsetPrintIndex(buf, icp);
312 InsetCommandParams icp(code);
313 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
314 return new InsetLabel(buf, icp);
318 InsetCommandParams icp(code);
319 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
320 return new InsetLine(buf, icp);
323 case LISTINGS_CODE: {
324 InsetListingsParams par;
325 InsetListings::string2params(to_utf8(cmd.argument()), par);
326 return new InsetListings(buf, par);
330 InsetCommandParams icp(code);
331 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
332 return new InsetNomencl(buf, icp);
336 InsetCommandParams icp(code);
337 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
338 return new InsetRef(buf, icp);
342 InsetScriptParams isp;
343 InsetScript::string2params(to_utf8(cmd.argument()), isp);
344 return new InsetScript(buf, isp);
348 InsetSpaceParams isp;
349 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
350 return new InsetSpace(isp);
354 InsetCommandParams icp(code);
355 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
356 return new InsetTOC(buf, icp);
361 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
362 return new InsetVSpace(vspace);
366 return new InsetPreview(buf);
369 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
374 } //end LFUN_INSET_INSERT
376 case LFUN_SPACE_INSERT: {
377 string const name = cmd.getArg(0);
378 string const len = cmd.getArg(1);
380 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
383 InsetSpaceParams isp;
384 // The tests for isp.math might be disabled after a file format change
385 if (name == "normal")
386 isp.kind = InsetSpaceParams::NORMAL;
387 else if (name == "protected")
388 isp.kind = InsetSpaceParams::PROTECTED;
389 else if (name == "visible")
390 isp.kind = InsetSpaceParams::VISIBLE;
391 else if (name == "thin")
392 isp.kind = InsetSpaceParams::THIN;
393 else if (isp.math && name == "med")
394 isp.kind = InsetSpaceParams::MEDIUM;
395 else if (isp.math && name == "thick")
396 isp.kind = InsetSpaceParams::THICK;
397 else if (name == "quad")
398 isp.kind = InsetSpaceParams::QUAD;
399 else if (name == "qquad")
400 isp.kind = InsetSpaceParams::QQUAD;
401 else if (name == "enspace")
402 isp.kind = InsetSpaceParams::ENSPACE;
403 else if (name == "enskip")
404 isp.kind = InsetSpaceParams::ENSKIP;
405 else if (name == "negthinspace")
406 isp.kind = InsetSpaceParams::NEGTHIN;
407 else if (name == "negmedspace")
408 isp.kind = InsetSpaceParams::NEGMEDIUM;
409 else if (name == "negthickspace")
410 isp.kind = InsetSpaceParams::NEGTHICK;
411 else if (name == "hfill")
412 isp.kind = InsetSpaceParams::HFILL;
413 else if (name == "hfill*")
414 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
415 else if (name == "dotfill")
416 isp.kind = InsetSpaceParams::DOTFILL;
417 else if (name == "hrulefill")
418 isp.kind = InsetSpaceParams::HRULEFILL;
419 else if (name == "hspace") {
420 if (len.empty() || !isValidGlueLength(len)) {
421 lyxerr << "LyX function 'space-insert hspace' "
422 << "needs a valid length argument." << endl;
425 isp.kind = InsetSpaceParams::CUSTOM;
426 isp.length = GlueLength(len);
428 else if (name == "hspace*") {
429 if (len.empty() || !isValidGlueLength(len)) {
430 lyxerr << "LyX function 'space-insert hspace*' "
431 << "needs a valid length argument." << endl;
434 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
435 isp.length = GlueLength(len);
438 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
441 return new InsetSpace(isp);
449 } catch (ExceptionMessage const & message) {
450 if (message.type_ == ErrorException) {
451 // This should never happen!
452 Alert::error(message.title_, message.details_);
454 } else if (message.type_ == WarningException) {
455 Alert::warning(message.title_, message.details_);
464 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
466 Inset * inset = createInsetHelper(buf, cmd);
468 inset->setBuffer(*buf);
473 Inset * readInset(Lexer & lex, Buffer * buf)
476 if (lex.getString() != "\\begin_inset")
477 LYXERR0("Buffer::readInset: Consistency check failed.");
479 auto_ptr<Inset> inset;
484 // test the different insets
486 // FIXME It would be better if we did not have this branch and could
487 // just do one massive switch for all insets. But at present, it's
488 // easier to do it this way, and we can't do the massive switch until
489 // the conversion mentioned below. Note that if we do want to do a
490 // single switch, we need to remove this "CommandInset" line---or
491 // replace it with a single "InsetType" line that would be used in all
493 if (tmptok == "CommandInset") {
495 string const insetType = lex.getString();
496 lex.pushToken(insetType);
498 InsetCode const code = insetCode(insetType);
500 //FIXME If we do the one massive switch, we cannot do this here, since
501 //we do not know in advance that we're dealing with a command inset.
502 //Worst case, we could put it in each case below. Better, we could
503 //pass the lexer to the constructor and let the params be built there.
504 InsetCommandParams inscmd(code);
509 inset.reset(new InsetBibitem(buf, inscmd));
512 inset.reset(new InsetBibtex(buf, inscmd));
515 inset.reset(new InsetCitation(buf, inscmd));
518 inset.reset(new InsetHyperlink(buf, inscmd));
521 inset.reset(new InsetInclude(buf, inscmd));
523 case INDEX_PRINT_CODE:
524 inset.reset(new InsetPrintIndex(buf, inscmd));
527 inset.reset(new InsetLabel(buf, inscmd));
530 inset.reset(new InsetLine(buf, inscmd));
533 inset.reset(new InsetNomencl(buf, inscmd));
535 case NOMENCL_PRINT_CODE:
536 inset.reset(new InsetPrintNomencl(buf, inscmd));
539 if (inscmd["name"].empty() && inscmd["reference"].empty())
541 inset.reset(new InsetRef(buf, inscmd));
544 inset.reset(new InsetTOC(buf, inscmd));
548 lyxerr << "unknown CommandInset '" << insetType
550 while (lex.isOK() && lex.getString() != "\\end_inset")
554 inset->setBuffer(*buf);
556 // FIXME This branch should be made to use inset codes as the preceding
557 // branch does. Unfortunately, that will take some doing. It requires
558 // converting the representation of the insets in LyX files so that they
559 // use the inset names listed in Inset.cpp. Then, as above, the inset names
560 // can be translated to inset codes using insetCode(). And the insets'
561 // write() routines should use insetName() rather than hardcoding it.
562 if (tmptok == "Quotes") {
563 inset.reset(new InsetQuotes(buf));
564 } else if (tmptok == "External") {
565 inset.reset(new InsetExternal(buf));
566 } else if (tmptok == "FormulaMacro") {
567 inset.reset(new MathMacroTemplate(buf));
568 } else if (tmptok == "Formula") {
569 inset.reset(new InsetMathHull(buf));
570 } else if (tmptok == "Graphics") {
571 inset.reset(new InsetGraphics(buf));
572 } else if (tmptok == "Note") {
573 inset.reset(new InsetNote(buf, tmptok));
574 } else if (tmptok == "Box") {
575 inset.reset(new InsetBox(buf, tmptok));
576 } else if (tmptok == "Flex") {
578 string s = lex.getString();
579 inset.reset(new InsetFlex(buf, s));
580 } else if (tmptok == "Branch") {
581 inset.reset(new InsetBranch(buf, InsetBranchParams()));
582 } else if (tmptok == "Phantom") {
583 inset.reset(new InsetPhantom(buf, tmptok));
584 } else if (tmptok == "ERT") {
585 inset.reset(new InsetERT(buf));
586 } else if (tmptok == "listings") {
587 inset.reset(new InsetListings(buf));
588 } else if (tmptok == "script") {
589 inset.reset(new InsetScript(buf));
590 } else if (tmptok == "space") {
591 inset.reset(new InsetSpace);
592 } else if (tmptok == "Tabular") {
593 inset.reset(new InsetTabular(buf));
594 } else if (tmptok == "Text") {
595 inset.reset(new InsetText(buf));
596 } else if (tmptok == "VSpace") {
597 inset.reset(new InsetVSpace);
598 } else if (tmptok == "Foot") {
599 inset.reset(new InsetFoot(buf));
600 } else if (tmptok == "Marginal") {
601 inset.reset(new InsetMarginal(buf));
602 } else if (tmptok == "Newpage") {
603 inset.reset(new InsetNewpage);
604 } else if (tmptok == "Newline") {
605 inset.reset(new InsetNewline);
606 } else if (tmptok == "Argument") {
607 inset.reset(new InsetArgument(buf));
608 } else if (tmptok == "Float") {
609 inset.reset(new InsetFloat(buf, string()));
610 } else if (tmptok == "Wrap") {
612 string tmptok = lex.getString();
613 inset.reset(new InsetWrap(buf, tmptok));
614 } else if (tmptok == "Caption") {
615 inset.reset(new InsetCaption(buf));
616 } else if (tmptok == "Index") {
617 inset.reset(new InsetIndex(buf, InsetIndexParams()));
618 } else if (tmptok == "FloatList") {
619 inset.reset(new InsetFloatList(buf));
620 } else if (tmptok == "Info") {
621 inset.reset(new InsetInfo(buf));
622 } else if (tmptok == "IPA") {
623 inset.reset(new InsetIPA(buf));
624 } else if (tmptok == "Preview") {
625 inset.reset(new InsetPreview(buf));
627 lyxerr << "unknown Inset type '" << tmptok
629 while (lex.isOK() && lex.getString() != "\\end_inset")
634 // Set the buffer reference for proper parsing of some insets
635 // (InsetCollapsable for example)
636 inset->setBuffer(*buf);
638 // Set again the buffer for insets that are created inside this inset
639 // (InsetMathHull for example).
640 inset->setBuffer(*buf);
642 return inset.release();