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 return new InsetArgument(buf);
160 case LFUN_FLOAT_INSERT: {
161 string argument = to_utf8(cmd.argument());
162 if (!argument.empty()) {
163 if (!contains(argument, "sideways")) {
164 if (!contains(argument, "wide"))
165 argument += "\nwide false";
166 argument += "\nsideways false";
169 return new InsetFloat(buf, argument);
172 case LFUN_FLOAT_WIDE_INSERT: {
173 string argument = to_utf8(cmd.argument());
174 if (!argument.empty()) {
175 if (!contains(argument, "sideways")) {
176 if (!contains(argument, "wide"))
177 argument += "\nwide true";
178 argument += "\nsideways false";
181 InsetFloat * fl = new InsetFloat(buf, argument);
186 case LFUN_WRAP_INSERT: {
187 string const argument = to_utf8(cmd.argument());
188 if (argument == "figure" || argument == "table")
189 return new InsetWrap(buf, argument);
190 lyxerr << "Non-existent wrapfig type: " << argument << endl;
194 case LFUN_INDEX_INSERT: {
195 docstring arg = cmd.argument();
196 return new InsetIndex(buf, InsetIndexParams(arg));
199 case LFUN_IPA_INSERT:
200 return new InsetIPA(buf);
202 case LFUN_NOMENCL_INSERT: {
203 InsetCommandParams icp(NOMENCL_CODE);
204 icp["symbol"] = cmd.argument();
205 return new InsetNomencl(buf, icp);
208 case LFUN_TABULAR_INSERT: {
209 if (cmd.argument().empty())
211 istringstream ss(to_utf8(cmd.argument()));
218 return new InsetTabular(buf, r, c);
221 case LFUN_CAPTION_INSERT:
222 return new InsetCaption(buf);
224 case LFUN_INDEX_PRINT: {
225 InsetCommandParams icp(INDEX_PRINT_CODE);
226 icp["type"] = cmd.argument();
227 return new InsetPrintIndex(buf, icp);
230 case LFUN_NOMENCL_PRINT: {
231 InsetCommandParams icp(NOMENCL_PRINT_CODE);
232 icp["set_width"] = from_ascii("auto");
233 return new InsetPrintNomencl(buf, icp);
236 case LFUN_INFO_INSERT: {
237 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
242 case LFUN_PREVIEW_INSERT:
243 return new InsetPreview(buf);
245 case LFUN_SCRIPT_INSERT: {
246 InsetScriptParams isp;
247 InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
248 return new InsetScript(buf, isp);
251 case LFUN_INSET_INSERT: {
252 string const name = cmd.getArg(0);
253 InsetCode code = insetCode(name);
256 lyxerr << "No such inset '" << name << "'.";
260 InsetCommandParams icp(code);
261 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
262 return new InsetBibitem(buf, icp);
266 InsetCommandParams icp(code);
267 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
268 return new InsetBibtex(buf, icp);
272 InsetCommandParams icp(code);
273 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
274 return new InsetCitation(buf, icp);
278 return new InsetERT(buf,
279 InsetERT::string2params(to_utf8(cmd.argument())));
282 case EXTERNAL_CODE: {
283 InsetExternalParams iep;
284 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
285 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
286 inset->setBuffer(*buf);
287 inset->setParams(iep);
288 return inset.release();
291 case GRAPHICS_CODE: {
292 InsetGraphicsParams igp;
293 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
294 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
295 inset->setParams(igp);
296 return inset.release();
299 case HYPERLINK_CODE: {
300 InsetCommandParams icp(code);
301 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
302 return new InsetHyperlink(buf, icp);
306 InsetCommandParams icp(code);
307 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
308 return new InsetInclude(buf, icp);
312 docstring arg = cmd.argument();
313 return new InsetIndex(buf, InsetIndexParams(arg));
316 case INDEX_PRINT_CODE: {
317 InsetCommandParams icp(code);
318 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
319 return new InsetPrintIndex(buf, icp);
323 InsetCommandParams icp(code);
324 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
325 return new InsetLabel(buf, icp);
329 InsetCommandParams icp(code);
330 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
331 return new InsetLine(buf, icp);
334 case LISTINGS_CODE: {
335 InsetListingsParams par;
336 InsetListings::string2params(to_utf8(cmd.argument()), par);
337 return new InsetListings(buf, par);
341 InsetCommandParams icp(code);
342 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
343 return new InsetNomencl(buf, icp);
347 InsetCommandParams icp(code);
348 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
349 return new InsetRef(buf, icp);
353 InsetScriptParams isp;
354 InsetScript::string2params(to_utf8(cmd.argument()), isp);
355 return new InsetScript(buf, isp);
359 InsetSpaceParams isp;
360 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
361 return new InsetSpace(isp);
365 InsetCommandParams icp(code);
366 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
367 return new InsetTOC(buf, icp);
372 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
373 return new InsetVSpace(vspace);
377 return new InsetPreview(buf);
380 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
385 } //end LFUN_INSET_INSERT
387 case LFUN_SPACE_INSERT: {
388 string const name = cmd.getArg(0);
389 string const len = cmd.getArg(1);
391 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
394 InsetSpaceParams isp;
395 // The tests for isp.math might be disabled after a file format change
396 if (name == "normal")
397 isp.kind = InsetSpaceParams::NORMAL;
398 else if (name == "protected")
399 isp.kind = InsetSpaceParams::PROTECTED;
400 else if (name == "visible")
401 isp.kind = InsetSpaceParams::VISIBLE;
402 else if (name == "thin")
403 isp.kind = InsetSpaceParams::THIN;
404 else if (isp.math && name == "med")
405 isp.kind = InsetSpaceParams::MEDIUM;
406 else if (isp.math && name == "thick")
407 isp.kind = InsetSpaceParams::THICK;
408 else if (name == "quad")
409 isp.kind = InsetSpaceParams::QUAD;
410 else if (name == "qquad")
411 isp.kind = InsetSpaceParams::QQUAD;
412 else if (name == "enspace")
413 isp.kind = InsetSpaceParams::ENSPACE;
414 else if (name == "enskip")
415 isp.kind = InsetSpaceParams::ENSKIP;
416 else if (name == "negthinspace")
417 isp.kind = InsetSpaceParams::NEGTHIN;
418 else if (name == "negmedspace")
419 isp.kind = InsetSpaceParams::NEGMEDIUM;
420 else if (name == "negthickspace")
421 isp.kind = InsetSpaceParams::NEGTHICK;
422 else if (name == "hfill")
423 isp.kind = InsetSpaceParams::HFILL;
424 else if (name == "hfill*")
425 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
426 else if (name == "dotfill")
427 isp.kind = InsetSpaceParams::DOTFILL;
428 else if (name == "hrulefill")
429 isp.kind = InsetSpaceParams::HRULEFILL;
430 else if (name == "hspace") {
431 if (len.empty() || !isValidGlueLength(len)) {
432 lyxerr << "LyX function 'space-insert hspace' "
433 << "needs a valid length argument." << endl;
436 isp.kind = InsetSpaceParams::CUSTOM;
437 isp.length = GlueLength(len);
439 else if (name == "hspace*") {
440 if (len.empty() || !isValidGlueLength(len)) {
441 lyxerr << "LyX function 'space-insert hspace*' "
442 << "needs a valid length argument." << endl;
445 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
446 isp.length = GlueLength(len);
449 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
452 return new InsetSpace(isp);
460 } catch (ExceptionMessage const & message) {
461 if (message.type_ == ErrorException) {
462 // This should never happen!
463 Alert::error(message.title_, message.details_);
465 } else if (message.type_ == WarningException) {
466 Alert::warning(message.title_, message.details_);
475 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
477 Inset * inset = createInsetHelper(buf, cmd);
479 inset->setBuffer(*buf);
484 Inset * readInset(Lexer & lex, Buffer * buf)
487 if (lex.getString() != "\\begin_inset")
488 LYXERR0("Buffer::readInset: Consistency check failed.");
490 auto_ptr<Inset> inset;
495 // test the different insets
497 // FIXME It would be better if we did not have this branch and could
498 // just do one massive switch for all insets. But at present, it's
499 // easier to do it this way, and we can't do the massive switch until
500 // the conversion mentioned below. Note that if we do want to do a
501 // single switch, we need to remove this "CommandInset" line---or
502 // replace it with a single "InsetType" line that would be used in all
504 if (tmptok == "CommandInset") {
506 string const insetType = lex.getString();
507 lex.pushToken(insetType);
509 InsetCode const code = insetCode(insetType);
511 //FIXME If we do the one massive switch, we cannot do this here, since
512 //we do not know in advance that we're dealing with a command inset.
513 //Worst case, we could put it in each case below. Better, we could
514 //pass the lexer to the constructor and let the params be built there.
515 InsetCommandParams inscmd(code);
520 inset.reset(new InsetBibitem(buf, inscmd));
523 inset.reset(new InsetBibtex(buf, inscmd));
526 inset.reset(new InsetCitation(buf, inscmd));
529 inset.reset(new InsetHyperlink(buf, inscmd));
532 inset.reset(new InsetInclude(buf, inscmd));
534 case INDEX_PRINT_CODE:
535 inset.reset(new InsetPrintIndex(buf, inscmd));
538 inset.reset(new InsetLabel(buf, inscmd));
541 inset.reset(new InsetLine(buf, inscmd));
544 inset.reset(new InsetNomencl(buf, inscmd));
546 case NOMENCL_PRINT_CODE:
547 inset.reset(new InsetPrintNomencl(buf, inscmd));
550 if (inscmd["name"].empty() && inscmd["reference"].empty())
552 inset.reset(new InsetRef(buf, inscmd));
555 inset.reset(new InsetTOC(buf, inscmd));
559 lyxerr << "unknown CommandInset '" << insetType
561 while (lex.isOK() && lex.getString() != "\\end_inset")
565 inset->setBuffer(*buf);
567 // FIXME This branch should be made to use inset codes as the preceding
568 // branch does. Unfortunately, that will take some doing. It requires
569 // converting the representation of the insets in LyX files so that they
570 // use the inset names listed in Inset.cpp. Then, as above, the inset names
571 // can be translated to inset codes using insetCode(). And the insets'
572 // write() routines should use insetName() rather than hardcoding it.
573 if (tmptok == "Quotes") {
574 inset.reset(new InsetQuotes(buf));
575 } else if (tmptok == "External") {
576 inset.reset(new InsetExternal(buf));
577 } else if (tmptok == "FormulaMacro") {
578 inset.reset(new MathMacroTemplate(buf));
579 } else if (tmptok == "Formula") {
580 inset.reset(new InsetMathHull(buf));
581 } else if (tmptok == "Graphics") {
582 inset.reset(new InsetGraphics(buf));
583 } else if (tmptok == "Note") {
584 inset.reset(new InsetNote(buf, tmptok));
585 } else if (tmptok == "Box") {
586 inset.reset(new InsetBox(buf, tmptok));
587 } else if (tmptok == "Flex") {
589 string s = lex.getString();
590 inset.reset(new InsetFlex(buf, s));
591 } else if (tmptok == "Branch") {
592 inset.reset(new InsetBranch(buf, InsetBranchParams()));
593 } else if (tmptok == "Phantom") {
594 inset.reset(new InsetPhantom(buf, tmptok));
595 } else if (tmptok == "ERT") {
596 inset.reset(new InsetERT(buf));
597 } else if (tmptok == "listings") {
598 inset.reset(new InsetListings(buf));
599 } else if (tmptok == "script") {
600 inset.reset(new InsetScript(buf));
601 } else if (tmptok == "space") {
602 inset.reset(new InsetSpace);
603 } else if (tmptok == "Tabular") {
604 inset.reset(new InsetTabular(buf));
605 } else if (tmptok == "Text") {
606 inset.reset(new InsetText(buf));
607 } else if (tmptok == "VSpace") {
608 inset.reset(new InsetVSpace);
609 } else if (tmptok == "Foot") {
610 inset.reset(new InsetFoot(buf));
611 } else if (tmptok == "Marginal") {
612 inset.reset(new InsetMarginal(buf));
613 } else if (tmptok == "Newpage") {
614 inset.reset(new InsetNewpage);
615 } else if (tmptok == "Newline") {
616 inset.reset(new InsetNewline);
617 } else if (tmptok == "Argument") {
618 inset.reset(new InsetArgument(buf));
619 } else if (tmptok == "Float") {
620 inset.reset(new InsetFloat(buf, string()));
621 } else if (tmptok == "Wrap") {
623 string tmptok = lex.getString();
624 inset.reset(new InsetWrap(buf, tmptok));
625 } else if (tmptok == "Caption") {
626 inset.reset(new InsetCaption(buf));
627 } else if (tmptok == "Index") {
628 inset.reset(new InsetIndex(buf, InsetIndexParams()));
629 } else if (tmptok == "FloatList") {
630 inset.reset(new InsetFloatList(buf));
631 } else if (tmptok == "Info") {
632 inset.reset(new InsetInfo(buf));
633 } else if (tmptok == "IPA") {
634 inset.reset(new InsetIPA(buf));
635 } else if (tmptok == "IPADeco") {
636 inset.reset(new InsetIPADeco(buf, tmptok));
637 } else if (tmptok == "Preview") {
638 inset.reset(new InsetPreview(buf));
640 lyxerr << "unknown Inset type '" << tmptok
642 while (lex.isOK() && lex.getString() != "\\end_inset")
647 // Set the buffer reference for proper parsing of some insets
648 // (InsetCollapsable for example)
649 inset->setBuffer(*buf);
651 // Set again the buffer for insets that are created inside this inset
652 // (InsetMathHull for example).
653 inset->setBuffer(*buf);
655 return inset.release();