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 return new InsetCaption(buf);
230 case LFUN_INDEX_PRINT: {
231 InsetCommandParams icp(INDEX_PRINT_CODE);
232 icp["type"] = cmd.argument();
233 return new InsetPrintIndex(buf, icp);
236 case LFUN_NOMENCL_PRINT: {
237 InsetCommandParams icp(NOMENCL_PRINT_CODE);
238 icp["set_width"] = from_ascii("auto");
239 return new InsetPrintNomencl(buf, icp);
242 case LFUN_INFO_INSERT: {
243 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
248 case LFUN_PREVIEW_INSERT:
249 return new InsetPreview(buf);
251 case LFUN_SCRIPT_INSERT: {
252 InsetScriptParams isp;
253 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
254 return new InsetScript(buf, isp);
257 case LFUN_INSET_INSERT: {
258 string const name = cmd.getArg(0);
259 InsetCode code = insetCode(name);
262 lyxerr << "No such inset '" << name << "'.";
266 InsetCommandParams icp(code);
267 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
268 return new InsetBibitem(buf, icp);
272 InsetCommandParams icp(code);
273 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
274 return new InsetBibtex(buf, icp);
278 InsetCommandParams icp(code);
279 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
280 return new InsetCitation(buf, icp);
284 return new InsetERT(buf,
285 InsetERT::string2params(to_utf8(cmd.argument())));
288 case EXTERNAL_CODE: {
289 InsetExternalParams iep;
290 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
291 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
292 inset->setBuffer(*buf);
293 inset->setParams(iep);
294 return inset.release();
297 case GRAPHICS_CODE: {
298 InsetGraphicsParams igp;
299 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
300 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
301 inset->setParams(igp);
302 return inset.release();
305 case HYPERLINK_CODE: {
306 InsetCommandParams icp(code);
307 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
308 return new InsetHyperlink(buf, icp);
312 InsetCommandParams icp(code);
313 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
314 return new InsetInclude(buf, icp);
318 docstring arg = cmd.argument();
319 return new InsetIndex(buf, InsetIndexParams(arg));
322 case INDEX_PRINT_CODE: {
323 InsetCommandParams icp(code);
324 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
325 return new InsetPrintIndex(buf, icp);
329 InsetCommandParams icp(code);
330 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
331 return new InsetLabel(buf, icp);
335 InsetCommandParams icp(code);
336 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
337 return new InsetLine(buf, icp);
340 case LISTINGS_CODE: {
341 InsetListingsParams par;
342 InsetListings::string2params(to_utf8(cmd.argument()), par);
343 return new InsetListings(buf, par);
347 InsetCommandParams icp(code);
348 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
349 return new InsetNomencl(buf, icp);
353 InsetCommandParams icp(code);
354 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
355 return new InsetRef(buf, icp);
359 InsetScriptParams isp;
360 InsetScript::string2params(to_utf8(cmd.argument()), isp);
361 return new InsetScript(buf, isp);
365 InsetSpaceParams isp;
366 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
367 return new InsetSpace(isp);
371 InsetCommandParams icp(code);
372 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
373 return new InsetTOC(buf, icp);
378 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
379 return new InsetVSpace(vspace);
383 return new InsetPreview(buf);
386 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
391 } //end LFUN_INSET_INSERT
393 case LFUN_SPACE_INSERT: {
394 string const name = cmd.getArg(0);
395 string const len = cmd.getArg(1);
397 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
400 InsetSpaceParams isp;
401 // The tests for isp.math might be disabled after a file format change
402 if (name == "normal")
403 isp.kind = InsetSpaceParams::NORMAL;
404 else if (name == "protected")
405 isp.kind = InsetSpaceParams::PROTECTED;
406 else if (name == "visible")
407 isp.kind = InsetSpaceParams::VISIBLE;
408 else if (name == "thin")
409 isp.kind = InsetSpaceParams::THIN;
410 else if (isp.math && name == "med")
411 isp.kind = InsetSpaceParams::MEDIUM;
412 else if (isp.math && name == "thick")
413 isp.kind = InsetSpaceParams::THICK;
414 else if (name == "quad")
415 isp.kind = InsetSpaceParams::QUAD;
416 else if (name == "qquad")
417 isp.kind = InsetSpaceParams::QQUAD;
418 else if (name == "enspace")
419 isp.kind = InsetSpaceParams::ENSPACE;
420 else if (name == "enskip")
421 isp.kind = InsetSpaceParams::ENSKIP;
422 else if (name == "negthinspace")
423 isp.kind = InsetSpaceParams::NEGTHIN;
424 else if (name == "negmedspace")
425 isp.kind = InsetSpaceParams::NEGMEDIUM;
426 else if (name == "negthickspace")
427 isp.kind = InsetSpaceParams::NEGTHICK;
428 else if (name == "hfill")
429 isp.kind = InsetSpaceParams::HFILL;
430 else if (name == "hfill*")
431 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
432 else if (name == "dotfill")
433 isp.kind = InsetSpaceParams::DOTFILL;
434 else if (name == "hrulefill")
435 isp.kind = InsetSpaceParams::HRULEFILL;
436 else if (name == "hspace") {
437 if (len.empty() || !isValidGlueLength(len)) {
438 lyxerr << "LyX function 'space-insert hspace' "
439 << "needs a valid length argument." << endl;
442 isp.kind = InsetSpaceParams::CUSTOM;
443 isp.length = GlueLength(len);
445 else if (name == "hspace*") {
446 if (len.empty() || !isValidGlueLength(len)) {
447 lyxerr << "LyX function 'space-insert hspace*' "
448 << "needs a valid length argument." << endl;
451 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
452 isp.length = GlueLength(len);
455 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
458 return new InsetSpace(isp);
466 } catch (ExceptionMessage const & message) {
467 if (message.type_ == ErrorException) {
468 // This should never happen!
469 Alert::error(message.title_, message.details_);
471 } else if (message.type_ == WarningException) {
472 Alert::warning(message.title_, message.details_);
481 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
483 Inset * inset = createInsetHelper(buf, cmd);
485 inset->setBuffer(*buf);
490 Inset * readInset(Lexer & lex, Buffer * buf)
493 if (lex.getString() != "\\begin_inset")
494 LYXERR0("Buffer::readInset: Consistency check failed.");
496 auto_ptr<Inset> inset;
501 // test the different insets
503 // FIXME It would be better if we did not have this branch and could
504 // just do one massive switch for all insets. But at present, it's
505 // easier to do it this way, and we can't do the massive switch until
506 // the conversion mentioned below. Note that if we do want to do a
507 // single switch, we need to remove this "CommandInset" line---or
508 // replace it with a single "InsetType" line that would be used in all
510 if (tmptok == "CommandInset") {
512 string const insetType = lex.getString();
513 lex.pushToken(insetType);
515 InsetCode const code = insetCode(insetType);
517 //FIXME If we do the one massive switch, we cannot do this here, since
518 //we do not know in advance that we're dealing with a command inset.
519 //Worst case, we could put it in each case below. Better, we could
520 //pass the lexer to the constructor and let the params be built there.
521 InsetCommandParams inscmd(code);
526 inset.reset(new InsetBibitem(buf, inscmd));
529 inset.reset(new InsetBibtex(buf, inscmd));
532 inset.reset(new InsetCitation(buf, inscmd));
535 inset.reset(new InsetHyperlink(buf, inscmd));
538 inset.reset(new InsetInclude(buf, inscmd));
540 case INDEX_PRINT_CODE:
541 inset.reset(new InsetPrintIndex(buf, inscmd));
544 inset.reset(new InsetLabel(buf, inscmd));
547 inset.reset(new InsetLine(buf, inscmd));
550 inset.reset(new InsetNomencl(buf, inscmd));
552 case NOMENCL_PRINT_CODE:
553 inset.reset(new InsetPrintNomencl(buf, inscmd));
556 if (inscmd["name"].empty() && inscmd["reference"].empty())
558 inset.reset(new InsetRef(buf, inscmd));
561 inset.reset(new InsetTOC(buf, inscmd));
565 lyxerr << "unknown CommandInset '" << insetType
567 while (lex.isOK() && lex.getString() != "\\end_inset")
571 inset->setBuffer(*buf);
573 // FIXME This branch should be made to use inset codes
574 // as the preceding branch does. Unfortunately, that
575 // will take some doing. It requires converting the
576 // representation of the insets in LyX files so that
577 // they use the inset names listed in Inset.cpp. Then,
578 // as above, the inset names can be translated to
579 // inset codes using insetCode(). And the insets'
580 // write() routines should use insetName() rather than
582 if (tmptok == "Quotes") {
583 inset.reset(new InsetQuotes(buf));
584 } else if (tmptok == "External") {
585 inset.reset(new InsetExternal(buf));
586 } else if (tmptok == "FormulaMacro") {
587 inset.reset(new MathMacroTemplate(buf));
588 } else if (tmptok == "Formula") {
589 inset.reset(new InsetMathHull(buf));
590 } else if (tmptok == "Graphics") {
591 inset.reset(new InsetGraphics(buf));
592 } else if (tmptok == "Note") {
593 inset.reset(new InsetNote(buf, tmptok));
594 } else if (tmptok == "Box") {
595 inset.reset(new InsetBox(buf, tmptok));
596 } else if (tmptok == "Flex") {
598 string s = lex.getString();
599 inset.reset(new InsetFlex(buf, s));
600 } else if (tmptok == "Branch") {
601 inset.reset(new InsetBranch(buf, InsetBranchParams()));
602 } else if (tmptok == "Phantom") {
603 inset.reset(new InsetPhantom(buf, tmptok));
604 } else if (tmptok == "ERT") {
605 inset.reset(new InsetERT(buf));
606 } else if (tmptok == "listings") {
607 inset.reset(new InsetListings(buf));
608 } else if (tmptok == "script") {
609 inset.reset(new InsetScript(buf));
610 } else if (tmptok == "space") {
611 inset.reset(new InsetSpace);
612 } else if (tmptok == "Tabular") {
613 inset.reset(new InsetTabular(buf));
614 } else if (tmptok == "Text") {
615 inset.reset(new InsetText(buf));
616 } else if (tmptok == "VSpace") {
617 inset.reset(new InsetVSpace);
618 } else if (tmptok == "Foot") {
619 inset.reset(new InsetFoot(buf));
620 } else if (tmptok == "Marginal") {
621 inset.reset(new InsetMarginal(buf));
622 } else if (tmptok == "Newpage") {
623 inset.reset(new InsetNewpage);
624 } else if (tmptok == "Newline") {
625 inset.reset(new InsetNewline);
626 } else if (tmptok == "Argument") {
627 inset.reset(new InsetArgument(buf, tmptok));
628 } else if (tmptok == "Float") {
629 inset.reset(new InsetFloat(buf, string()));
630 } else if (tmptok == "Wrap") {
632 string tmptok = lex.getString();
633 inset.reset(new InsetWrap(buf, tmptok));
634 } else if (tmptok == "Caption") {
635 inset.reset(new InsetCaption(buf));
636 } else if (tmptok == "Index") {
637 inset.reset(new InsetIndex(buf, InsetIndexParams()));
638 } else if (tmptok == "FloatList") {
639 inset.reset(new InsetFloatList(buf));
640 } else if (tmptok == "Info") {
641 inset.reset(new InsetInfo(buf));
642 } else if (tmptok == "IPA") {
643 inset.reset(new InsetIPA(buf));
644 } else if (tmptok == "IPADeco") {
645 inset.reset(new InsetIPADeco(buf, tmptok));
646 } else if (tmptok == "Preview") {
647 inset.reset(new InsetPreview(buf));
649 lyxerr << "unknown Inset type '" << tmptok
651 while (lex.isOK() && lex.getString() != "\\end_inset")
656 // Set the buffer reference for proper parsing of some insets
657 // (InsetCollapsable for example)
658 inset->setBuffer(*buf);
660 // Set again the buffer for insets that are created inside this inset
661 // (InsetMathHull for example).
662 inset->setBuffer(*buf);
664 return inset.release();