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/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/MathMacroTemplate.h"
62 #include "mathed/InsetMathHull.h"
64 #include "frontends/alert.h"
66 #include "support/debug.h"
67 #include "support/lstrings.h"
68 #include "support/ExceptionMessage.h"
70 #include "support/lassert.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 return new InsetNewpage(inp);
102 case LFUN_FLEX_INSERT: {
103 string s = cmd.getArg(0);
104 return new InsetFlex(buf, s);
107 case LFUN_NOTE_INSERT: {
108 string arg = cmd.getArg(0);
111 return new InsetNote(buf, arg);
114 case LFUN_BOX_INSERT: {
115 string arg = cmd.getArg(0);
118 return new InsetBox(buf, arg);
121 case LFUN_BRANCH_INSERT: {
122 docstring arg = cmd.argument();
124 arg = from_ascii("none");
125 return new InsetBranch(buf, InsetBranchParams(arg));
128 case LFUN_PHANTOM_INSERT: {
129 string arg = cmd.getArg(0);
132 return new InsetPhantom(buf, arg);
135 case LFUN_IPAMACRO_INSERT: {
136 string const arg1 = cmd.getArg(0);
137 string const arg2 = cmd.getArg(1);
138 if (arg1 != "deco") {
139 LYXERR0("LFUN_IPAMACRO_INSERT: wrong argument");
142 return new InsetIPADeco(buf, arg2);
145 case LFUN_ERT_INSERT:
146 return new InsetERT(buf);
148 case LFUN_LISTING_INSERT:
149 return new InsetListings(buf);
151 case LFUN_FOOTNOTE_INSERT:
152 return new InsetFoot(buf);
154 case LFUN_MARGINALNOTE_INSERT:
155 return new InsetMarginal(buf);
157 case LFUN_ARGUMENT_INSERT: {
158 string arg = cmd.getArg(0);
160 LYXERR0("argument-insert needs an argument!");
163 return new InsetArgument(buf, arg);
166 case LFUN_FLOAT_INSERT: {
167 string argument = to_utf8(cmd.argument());
168 if (!argument.empty()) {
169 if (!contains(argument, "sideways")) {
170 if (!contains(argument, "wide"))
171 argument += "\nwide false";
172 argument += "\nsideways false";
175 return new InsetFloat(buf, argument);
178 case LFUN_FLOAT_WIDE_INSERT: {
179 string argument = to_utf8(cmd.argument());
180 if (!argument.empty()) {
181 if (!contains(argument, "sideways")) {
182 if (!contains(argument, "wide"))
183 argument += "\nwide true";
184 argument += "\nsideways false";
187 InsetFloat * fl = new InsetFloat(buf, argument);
192 case LFUN_WRAP_INSERT: {
193 string const argument = to_utf8(cmd.argument());
194 if (argument == "figure" || argument == "table")
195 return new InsetWrap(buf, argument);
196 lyxerr << "Non-existent wrapfig type: " << argument << endl;
200 case LFUN_INDEX_INSERT: {
201 docstring arg = cmd.argument();
202 return new InsetIndex(buf, InsetIndexParams(arg));
205 case LFUN_IPA_INSERT:
206 return new InsetIPA(buf);
208 case LFUN_NOMENCL_INSERT: {
209 InsetCommandParams icp(NOMENCL_CODE);
210 icp["symbol"] = cmd.argument();
211 return new InsetNomencl(buf, icp);
214 case LFUN_TABULAR_INSERT: {
215 if (cmd.argument().empty())
217 istringstream ss(to_utf8(cmd.argument()));
224 return new InsetTabular(buf, r, c);
227 case LFUN_CAPTION_INSERT: {
228 string arg = cmd.getArg(0);
231 return new InsetCaption(buf, arg);
234 case LFUN_INDEX_PRINT: {
235 InsetCommandParams icp(INDEX_PRINT_CODE);
236 icp["type"] = cmd.argument();
237 return new InsetPrintIndex(buf, icp);
240 case LFUN_NOMENCL_PRINT: {
241 InsetCommandParams icp(NOMENCL_PRINT_CODE);
242 icp["set_width"] = from_ascii("auto");
243 return new InsetPrintNomencl(buf, icp);
246 case LFUN_INFO_INSERT: {
247 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
252 case LFUN_PREVIEW_INSERT:
253 return new InsetPreview(buf);
255 case LFUN_SCRIPT_INSERT: {
256 InsetScriptParams isp;
257 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
258 return new InsetScript(buf, isp);
261 case LFUN_INSET_INSERT: {
262 string const name = cmd.getArg(0);
263 InsetCode code = insetCode(name);
266 lyxerr << "No such inset '" << name << "'.";
270 InsetCommandParams icp(code);
271 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
272 return new InsetBibitem(buf, icp);
276 InsetCommandParams icp(code);
277 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
278 return new InsetBibtex(buf, icp);
282 InsetCommandParams icp(code);
283 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
284 return new InsetCitation(buf, icp);
288 return new InsetERT(buf,
289 InsetERT::string2params(to_utf8(cmd.argument())));
292 case EXTERNAL_CODE: {
293 InsetExternalParams iep;
294 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
295 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
296 inset->setBuffer(*buf);
297 inset->setParams(iep);
298 return inset.release();
301 case GRAPHICS_CODE: {
302 InsetGraphicsParams igp;
303 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
304 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
305 inset->setParams(igp);
306 return inset.release();
309 case HYPERLINK_CODE: {
310 InsetCommandParams icp(code);
311 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
312 return new InsetHyperlink(buf, icp);
316 InsetCommandParams icp(code);
317 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
318 return new InsetInclude(buf, icp);
322 docstring arg = cmd.argument();
323 return new InsetIndex(buf, InsetIndexParams(arg));
326 case INDEX_PRINT_CODE: {
327 InsetCommandParams icp(code);
328 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
329 return new InsetPrintIndex(buf, icp);
333 InsetCommandParams icp(code);
334 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
335 return new InsetLabel(buf, icp);
339 InsetCommandParams icp(code);
340 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
341 return new InsetLine(buf, icp);
344 case LISTINGS_CODE: {
345 InsetListingsParams par;
346 InsetListings::string2params(to_utf8(cmd.argument()), par);
347 return new InsetListings(buf, par);
351 InsetCommandParams icp(code);
352 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
353 return new InsetNomencl(buf, icp);
357 InsetCommandParams icp(code);
358 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
359 return new InsetRef(buf, icp);
363 InsetScriptParams isp;
364 InsetScript::string2params(to_utf8(cmd.argument()), isp);
365 return new InsetScript(buf, isp);
369 InsetSpaceParams isp;
370 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
371 return new InsetSpace(isp);
375 InsetCommandParams icp(code);
376 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
377 return new InsetTOC(buf, icp);
382 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
383 return new InsetVSpace(vspace);
387 return new InsetPreview(buf);
390 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
395 } //end LFUN_INSET_INSERT
397 case LFUN_SPACE_INSERT: {
398 string const name = cmd.getArg(0);
399 string const len = cmd.getArg(1);
401 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
404 InsetSpaceParams isp;
405 // The tests for isp.math might be disabled after a file format change
406 if (name == "normal")
407 isp.kind = InsetSpaceParams::NORMAL;
408 else if (name == "protected")
409 isp.kind = InsetSpaceParams::PROTECTED;
410 else if (name == "visible")
411 isp.kind = InsetSpaceParams::VISIBLE;
412 else if (name == "thin")
413 isp.kind = InsetSpaceParams::THIN;
414 else if (isp.math && name == "med")
415 isp.kind = InsetSpaceParams::MEDIUM;
416 else if (isp.math && name == "thick")
417 isp.kind = InsetSpaceParams::THICK;
418 else if (name == "quad")
419 isp.kind = InsetSpaceParams::QUAD;
420 else if (name == "qquad")
421 isp.kind = InsetSpaceParams::QQUAD;
422 else if (name == "enspace")
423 isp.kind = InsetSpaceParams::ENSPACE;
424 else if (name == "enskip")
425 isp.kind = InsetSpaceParams::ENSKIP;
426 else if (name == "negthinspace")
427 isp.kind = InsetSpaceParams::NEGTHIN;
428 else if (name == "negmedspace")
429 isp.kind = InsetSpaceParams::NEGMEDIUM;
430 else if (name == "negthickspace")
431 isp.kind = InsetSpaceParams::NEGTHICK;
432 else if (name == "hfill")
433 isp.kind = InsetSpaceParams::HFILL;
434 else if (name == "hfill*")
435 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
436 else if (name == "dotfill")
437 isp.kind = InsetSpaceParams::DOTFILL;
438 else if (name == "hrulefill")
439 isp.kind = InsetSpaceParams::HRULEFILL;
440 else if (name == "hspace") {
441 if (len.empty() || !isValidGlueLength(len)) {
442 lyxerr << "LyX function 'space-insert hspace' "
443 << "needs a valid length argument." << endl;
446 isp.kind = InsetSpaceParams::CUSTOM;
447 isp.length = GlueLength(len);
449 else if (name == "hspace*") {
450 if (len.empty() || !isValidGlueLength(len)) {
451 lyxerr << "LyX function 'space-insert hspace*' "
452 << "needs a valid length argument." << endl;
455 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
456 isp.length = GlueLength(len);
459 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
462 return new InsetSpace(isp);
470 } catch (ExceptionMessage const & message) {
471 if (message.type_ == ErrorException) {
472 // This should never happen!
473 Alert::error(message.title_, message.details_);
475 } else if (message.type_ == WarningException) {
476 Alert::warning(message.title_, message.details_);
485 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
487 Inset * inset = createInsetHelper(buf, cmd);
489 inset->setBuffer(*buf);
494 Inset * readInset(Lexer & lex, Buffer * buf)
497 if (lex.getString() != "\\begin_inset")
498 LYXERR0("Buffer::readInset: Consistency check failed.");
500 auto_ptr<Inset> inset;
505 // test the different insets
507 // FIXME It would be better if we did not have this branch and could
508 // just do one massive switch for all insets. But at present, it's
509 // easier to do it this way, and we can't do the massive switch until
510 // the conversion mentioned below. Note that if we do want to do a
511 // single switch, we need to remove this "CommandInset" line---or
512 // replace it with a single "InsetType" line that would be used in all
514 if (tmptok == "CommandInset") {
516 string const insetType = lex.getString();
517 lex.pushToken(insetType);
519 InsetCode const code = insetCode(insetType);
521 //FIXME If we do the one massive switch, we cannot do this here, since
522 //we do not know in advance that we're dealing with a command inset.
523 //Worst case, we could put it in each case below. Better, we could
524 //pass the lexer to the constructor and let the params be built there.
525 InsetCommandParams inscmd(code);
530 inset.reset(new InsetBibitem(buf, inscmd));
533 inset.reset(new InsetBibtex(buf, inscmd));
536 inset.reset(new InsetCitation(buf, inscmd));
539 inset.reset(new InsetHyperlink(buf, inscmd));
542 inset.reset(new InsetInclude(buf, inscmd));
544 case INDEX_PRINT_CODE:
545 inset.reset(new InsetPrintIndex(buf, inscmd));
548 inset.reset(new InsetLabel(buf, inscmd));
551 inset.reset(new InsetLine(buf, inscmd));
554 inset.reset(new InsetNomencl(buf, inscmd));
556 case NOMENCL_PRINT_CODE:
557 inset.reset(new InsetPrintNomencl(buf, inscmd));
560 if (inscmd["name"].empty() && inscmd["reference"].empty())
562 inset.reset(new InsetRef(buf, inscmd));
565 inset.reset(new InsetTOC(buf, inscmd));
569 lyxerr << "unknown CommandInset '" << insetType
571 while (lex.isOK() && lex.getString() != "\\end_inset")
575 inset->setBuffer(*buf);
577 // FIXME This branch should be made to use inset codes
578 // as the preceding branch does. Unfortunately, that
579 // will take some doing. It requires converting the
580 // representation of the insets in LyX files so that
581 // they use the inset names listed in Inset.cpp. Then,
582 // as above, the inset names can be translated to
583 // inset codes using insetCode(). And the insets'
584 // write() routines should use insetName() rather than
586 if (tmptok == "Quotes") {
587 inset.reset(new InsetQuotes(buf));
588 } else if (tmptok == "External") {
589 inset.reset(new InsetExternal(buf));
590 } else if (tmptok == "FormulaMacro") {
591 inset.reset(new MathMacroTemplate(buf));
592 } else if (tmptok == "Formula") {
593 inset.reset(new InsetMathHull(buf));
594 } else if (tmptok == "Graphics") {
595 inset.reset(new InsetGraphics(buf));
596 } else if (tmptok == "Note") {
597 inset.reset(new InsetNote(buf, tmptok));
598 } else if (tmptok == "Box") {
599 inset.reset(new InsetBox(buf, tmptok));
600 } else if (tmptok == "Flex") {
602 string s = lex.getString();
603 inset.reset(new InsetFlex(buf, s));
604 } else if (tmptok == "Branch") {
605 inset.reset(new InsetBranch(buf, InsetBranchParams()));
606 } else if (tmptok == "Phantom") {
607 inset.reset(new InsetPhantom(buf, tmptok));
608 } else if (tmptok == "ERT") {
609 inset.reset(new InsetERT(buf));
610 } else if (tmptok == "listings") {
611 inset.reset(new InsetListings(buf));
612 } else if (tmptok == "script") {
613 inset.reset(new InsetScript(buf));
614 } else if (tmptok == "space") {
615 inset.reset(new InsetSpace);
616 } else if (tmptok == "Tabular") {
617 inset.reset(new InsetTabular(buf));
618 } else if (tmptok == "Text") {
619 inset.reset(new InsetText(buf));
620 } else if (tmptok == "VSpace") {
621 inset.reset(new InsetVSpace);
622 } else if (tmptok == "Foot") {
623 inset.reset(new InsetFoot(buf));
624 } else if (tmptok == "Marginal") {
625 inset.reset(new InsetMarginal(buf));
626 } else if (tmptok == "Newpage") {
627 inset.reset(new InsetNewpage);
628 } else if (tmptok == "Newline") {
629 inset.reset(new InsetNewline);
630 } else if (tmptok == "Argument") {
631 inset.reset(new InsetArgument(buf, tmptok));
632 } else if (tmptok == "Float") {
633 inset.reset(new InsetFloat(buf, string()));
634 } else if (tmptok == "Wrap") {
636 string tmptok = lex.getString();
637 inset.reset(new InsetWrap(buf, tmptok));
638 } else if (tmptok == "Caption") {
640 string s = lex.getString();
641 inset.reset(new InsetCaption(buf, s));
642 } else if (tmptok == "Index") {
643 inset.reset(new InsetIndex(buf, InsetIndexParams()));
644 } else if (tmptok == "FloatList") {
645 inset.reset(new InsetFloatList(buf));
646 } else if (tmptok == "Info") {
647 inset.reset(new InsetInfo(buf));
648 } else if (tmptok == "IPA") {
649 inset.reset(new InsetIPA(buf));
650 } else if (tmptok == "IPADeco") {
651 inset.reset(new InsetIPADeco(buf, tmptok));
652 } else if (tmptok == "Preview") {
653 inset.reset(new InsetPreview(buf));
655 lyxerr << "unknown Inset type '" << tmptok
657 while (lex.isOK() && lex.getString() != "\\end_inset")
662 // Set the buffer reference for proper parsing of some insets
663 // (InsetCollapsable for example)
664 inset->setBuffer(*buf);
666 // Set again the buffer for insets that are created inside this inset
667 // (InsetMathHull for example).
668 inset->setBuffer(*buf);
670 return inset.release();