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/InsetCaption.h"
26 #include "insets/InsetCitation.h"
27 #include "insets/InsetFlex.h"
28 #include "insets/InsetERT.h"
29 #include "insets/InsetListings.h"
30 #include "insets/InsetExternal.h"
31 #include "insets/InsetFloat.h"
32 #include "insets/InsetFloatList.h"
33 #include "insets/InsetFoot.h"
34 #include "insets/InsetGraphics.h"
35 #include "insets/InsetInclude.h"
36 #include "insets/InsetIndex.h"
37 #include "insets/InsetInfo.h"
38 #include "insets/InsetNomencl.h"
39 #include "insets/InsetLabel.h"
40 #include "insets/InsetLine.h"
41 #include "insets/InsetMarginal.h"
42 #include "insets/InsetNewline.h"
43 #include "insets/InsetNewpage.h"
44 #include "insets/InsetNote.h"
45 #include "insets/InsetBox.h"
46 #include "insets/InsetBranch.h"
47 #include "insets/InsetPhantom.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetNewpage.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.h"
54 #include "insets/InsetHyperlink.h"
55 #include "insets/InsetVSpace.h"
56 #include "insets/InsetWrap.h"
58 #include "mathed/MathMacroTemplate.h"
59 #include "mathed/InsetMathHull.h"
61 #include "frontends/alert.h"
63 #include "support/debug.h"
64 #include "support/lstrings.h"
65 #include "support/ExceptionMessage.h"
67 #include "support/lassert.h"
72 using namespace lyx::support;
76 namespace Alert = frontend::Alert;
79 Inset * createInsetHelper(Buffer & buf, FuncRequest const & cmd)
81 BufferParams const & params = buf.params();
87 case LFUN_LINE_INSERT:
90 case LFUN_NEWPAGE_INSERT: {
91 string const name = cmd.getArg(0);
92 InsetNewpageParams inp;
93 if (name.empty() || name == "newpage")
94 inp.kind = InsetNewpageParams::NEWPAGE;
95 else if (name == "pagebreak")
96 inp.kind = InsetNewpageParams::PAGEBREAK;
97 else if (name == "clearpage")
98 inp.kind = InsetNewpageParams::CLEARPAGE;
99 else if (name == "cleardoublepage")
100 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
101 return new InsetNewpage(inp);
104 case LFUN_FLEX_INSERT: {
105 string s = cmd.getArg(0);
106 return new InsetFlex(buf, s);
109 case LFUN_NOTE_INSERT: {
110 string arg = cmd.getArg(0);
113 return new InsetNote(buf, arg);
116 case LFUN_BOX_INSERT: {
117 string arg = cmd.getArg(0);
120 return new InsetBox(buf, arg);
123 case LFUN_BRANCH_INSERT: {
124 docstring arg = cmd.argument();
126 arg = from_ascii("none");
127 return new InsetBranch(buf, InsetBranchParams(arg));
130 case LFUN_PHANTOM_INSERT: {
131 string arg = cmd.getArg(0);
134 return new InsetPhantom(buf, arg);
137 case LFUN_ERT_INSERT:
138 return new InsetERT(buf);
140 case LFUN_LISTING_INSERT:
141 return new InsetListings(buf);
143 case LFUN_FOOTNOTE_INSERT:
144 return new InsetFoot(buf);
146 case LFUN_MARGINALNOTE_INSERT:
147 return new InsetMarginal(buf);
149 case LFUN_OPTIONAL_INSERT:
150 return new InsetOptArg(buf);
152 case LFUN_FLOAT_INSERT: {
153 // check if the float type exists
154 string const argument = to_utf8(cmd.argument());
155 if (buf.params().documentClass().floats().typeExist(argument))
156 return new InsetFloat(buf, argument);
157 lyxerr << "Non-existent float type: " << argument << endl;
160 case LFUN_FLOAT_WIDE_INSERT: {
161 // check if the float type exists
162 string const argument = to_utf8(cmd.argument());
163 if (params.documentClass().floats().typeExist(argument)) {
164 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
165 p->setWide(true, params);
168 lyxerr << "Non-existent float type: " << argument << endl;
172 case LFUN_WRAP_INSERT: {
173 string const argument = to_utf8(cmd.argument());
174 if (argument == "figure" || argument == "table")
175 return new InsetWrap(buf, argument);
176 lyxerr << "Non-existent wrapfig type: " << argument << endl;
180 case LFUN_INDEX_INSERT: {
181 docstring arg = cmd.argument();
182 return new InsetIndex(buf, InsetIndexParams(arg));
185 case LFUN_NOMENCL_INSERT: {
186 InsetCommandParams icp(NOMENCL_CODE);
187 icp["symbol"] = cmd.argument();
188 return new InsetNomencl(icp);
191 case LFUN_TABULAR_INSERT: {
192 if (cmd.argument().empty())
194 istringstream ss(to_utf8(cmd.argument()));
201 return new InsetTabular(buf, r, c);
204 case LFUN_CAPTION_INSERT:
205 return new InsetCaption(buf);
207 case LFUN_INDEX_PRINT: {
208 InsetCommandParams icp(INDEX_PRINT_CODE);
209 icp["type"] = cmd.argument();
210 return new InsetPrintIndex(icp);
213 case LFUN_NOMENCL_PRINT:
214 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
216 case LFUN_TOC_INSERT:
217 return new InsetTOC(InsetCommandParams(TOC_CODE));
219 case LFUN_INFO_INSERT: {
220 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
225 case LFUN_INSET_INSERT: {
226 string const name = cmd.getArg(0);
227 InsetCode code = insetCode(name);
230 lyxerr << "No such inset '" << name << "'.";
234 InsetCommandParams icp(code);
235 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
236 return new InsetBibitem(buf, icp);
240 InsetCommandParams icp(code);
241 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
242 return new InsetBibtex(buf, icp);
246 InsetCommandParams icp(code);
247 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
248 return new InsetCitation(icp);
252 return new InsetERT(buf,
253 InsetERT::string2params(to_utf8(cmd.argument())));
256 case LISTINGS_CODE: {
257 InsetListingsParams par;
258 InsetListings::string2params(to_utf8(cmd.argument()), par);
259 return new InsetListings(buf, par);
262 case EXTERNAL_CODE: {
263 InsetExternalParams iep;
264 InsetExternal::string2params(to_utf8(cmd.argument()), buf, iep);
265 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
266 inset->setBuffer(buf);
267 inset->setParams(iep);
268 return inset.release();
271 case GRAPHICS_CODE: {
272 InsetGraphicsParams igp;
273 InsetGraphics::string2params(to_utf8(cmd.argument()), buf, igp);
274 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
275 inset->setParams(igp);
276 return inset.release();
279 case HYPERLINK_CODE: {
280 InsetCommandParams icp(code);
281 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
282 return new InsetHyperlink(icp);
286 InsetCommandParams icp(code);
287 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
288 return new InsetInclude(icp);
292 docstring arg = cmd.argument();
293 return new InsetIndex(buf, InsetIndexParams(arg));
296 case INDEX_PRINT_CODE: {
297 InsetCommandParams icp(code);
298 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
299 return new InsetPrintIndex(icp);
303 InsetCommandParams icp(code);
304 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
305 return new InsetNomencl(icp);
309 InsetCommandParams icp(code);
310 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
311 return new InsetLabel(icp);
315 InsetCommandParams icp(code);
316 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
317 return new InsetRef(buf, icp);
321 InsetSpaceParams isp;
322 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
323 return new InsetSpace(isp);
327 InsetCommandParams icp(code);
328 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
329 return new InsetTOC(icp);
334 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
335 return new InsetVSpace(vspace);
339 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
344 } //end LFUN_INSET_INSERT
346 case LFUN_SPACE_INSERT: {
347 string const name = cmd.getArg(0);
348 string const len = cmd.getArg(1);
350 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
353 InsetSpaceParams isp;
354 // The tests for isp.math might be disabled after a file format change
355 if (name == "normal")
356 isp.kind = InsetSpaceParams::NORMAL;
357 else if (name == "protected")
358 isp.kind = InsetSpaceParams::PROTECTED;
359 else if (name == "thin")
360 isp.kind = InsetSpaceParams::THIN;
361 else if (isp.math && name == "med")
362 isp.kind = InsetSpaceParams::MEDIUM;
363 else if (isp.math && name == "thick")
364 isp.kind = InsetSpaceParams::THICK;
365 else if (name == "quad")
366 isp.kind = InsetSpaceParams::QUAD;
367 else if (name == "qquad")
368 isp.kind = InsetSpaceParams::QQUAD;
369 else if (name == "enspace")
370 isp.kind = InsetSpaceParams::ENSPACE;
371 else if (name == "enskip")
372 isp.kind = InsetSpaceParams::ENSKIP;
373 else if (name == "negthinspace")
374 isp.kind = InsetSpaceParams::NEGTHIN;
375 else if (isp.math && name == "negmedspace")
376 isp.kind = InsetSpaceParams::NEGMEDIUM;
377 else if (isp.math && name == "negthickspace")
378 isp.kind = InsetSpaceParams::NEGTHICK;
379 else if (name == "hfill")
380 isp.kind = InsetSpaceParams::HFILL;
381 else if (name == "hfill*")
382 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
383 else if (name == "dotfill")
384 isp.kind = InsetSpaceParams::DOTFILL;
385 else if (name == "hrulefill")
386 isp.kind = InsetSpaceParams::HRULEFILL;
387 else if (name == "hspace") {
388 if (len.empty() || !isValidLength(len)) {
389 lyxerr << "LyX function 'space-insert hspace' "
390 << "needs a valid length argument." << endl;
393 isp.kind = InsetSpaceParams::CUSTOM;
394 isp.length = Length(len);
396 else if (name == "hspace*") {
397 if (len.empty() || !isValidLength(len)) {
398 lyxerr << "LyX function 'space-insert hspace*' "
399 << "needs a valid length argument." << endl;
402 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
403 isp.length = Length(len);
406 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
409 return new InsetSpace(isp);
417 } catch (ExceptionMessage const & message) {
418 if (message.type_ == ErrorException) {
419 // This should never happen!
420 Alert::error(message.title_, message.details_);
422 } else if (message.type_ == WarningException) {
423 Alert::warning(message.title_, message.details_);
432 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
434 Inset * inset = createInsetHelper(buf, cmd);
436 inset->setBuffer(buf);
441 Inset * readInset(Lexer & lex, Buffer const & buf)
444 if (lex.getString() != "\\begin_inset")
445 LYXERR0("Buffer::readInset: Consistency check failed.");
447 auto_ptr<Inset> inset;
452 // test the different insets
454 // FIXME It would be better if we did not have this branch and could
455 // just do one massive switch for all insets. But at present, it's
456 // easier to do it this way, and we can't do the massive switch until
457 // the conversion mentioned below. Note that if we do want to do a
458 // single switch, we need to remove this "CommandInset" line---or
459 // replace it with a single "InsetType" line that would be used in all
461 if (tmptok == "CommandInset") {
463 string const insetType = lex.getString();
464 lex.pushToken(insetType);
466 InsetCode const code = insetCode(insetType);
468 //FIXME If we do the one massive switch, we cannot do this here, since
469 //we do not know in advance that we're dealing with a command inset.
470 //Worst case, we could put it in each case below. Better, we could
471 //pass the lexer to the constructor and let the params be built there.
472 InsetCommandParams inscmd(code);
477 inset.reset(new InsetBibitem(buf, inscmd));
480 inset.reset(new InsetBibtex(buf, inscmd));
483 inset.reset(new InsetCitation(inscmd));
486 inset.reset(new InsetHyperlink(inscmd));
489 inset.reset(new InsetInclude(inscmd));
491 case INDEX_PRINT_CODE:
492 inset.reset(new InsetPrintIndex(inscmd));
495 inset.reset(new InsetLabel(inscmd));
498 inset.reset(new InsetNomencl(inscmd));
500 case NOMENCL_PRINT_CODE:
501 inset.reset(new InsetPrintNomencl(inscmd));
504 if (inscmd["name"].empty() && inscmd["reference"].empty())
506 inset.reset(new InsetRef(buf, inscmd));
509 inset.reset(new InsetTOC(inscmd));
513 lyxerr << "unknown CommandInset '" << insetType
515 while (lex.isOK() && lex.getString() != "\\end_inset")
519 inset->setBuffer(const_cast<Buffer &>(buf));
521 // FIXME This branch should be made to use inset codes as the preceding
522 // branch does. Unfortunately, that will take some doing. It requires
523 // converting the representation of the insets in LyX files so that they
524 // use the inset names listed in Inset.cpp. Then, as above, the inset names
525 // can be translated to inset codes using insetCode(). And the insets'
526 // write() routines should use insetName() rather than hardcoding it.
527 if (tmptok == "Quotes") {
528 inset.reset(new InsetQuotes(buf));
529 } else if (tmptok == "External") {
530 inset.reset(new InsetExternal(const_cast<Buffer &>(buf)));
531 } else if (tmptok == "FormulaMacro") {
532 inset.reset(new MathMacroTemplate);
533 } else if (tmptok == "Formula") {
534 inset.reset(new InsetMathHull);
535 } else if (tmptok == "Graphics") {
536 inset.reset(new InsetGraphics(const_cast<Buffer &>(buf)));
537 } else if (tmptok == "Note") {
538 inset.reset(new InsetNote(buf, tmptok));
539 } else if (tmptok == "Box") {
540 inset.reset(new InsetBox(buf, tmptok));
541 } else if (tmptok == "Flex") {
543 string s = lex.getString();
544 inset.reset(new InsetFlex(buf, s));
545 } else if (tmptok == "Branch") {
546 inset.reset(new InsetBranch(buf, InsetBranchParams()));
547 } else if (tmptok == "Phantom") {
548 inset.reset(new InsetPhantom(buf, tmptok));
549 } else if (tmptok == "ERT") {
550 inset.reset(new InsetERT(buf));
551 } else if (tmptok == "listings") {
552 inset.reset(new InsetListings(buf));
553 } else if (tmptok == "space") {
554 inset.reset(new InsetSpace);
555 } else if (tmptok == "Tabular") {
556 inset.reset(new InsetTabular(const_cast<Buffer &>(buf)));
557 } else if (tmptok == "Text") {
558 inset.reset(new InsetText(buf));
559 } else if (tmptok == "VSpace") {
560 inset.reset(new InsetVSpace);
561 } else if (tmptok == "Foot") {
562 inset.reset(new InsetFoot(buf));
563 } else if (tmptok == "Marginal") {
564 inset.reset(new InsetMarginal(buf));
565 } else if (tmptok == "Newpage") {
566 inset.reset(new InsetNewpage);
567 } else if (tmptok == "Newline") {
568 inset.reset(new InsetNewline);
569 } else if (tmptok == "OptArg") {
570 inset.reset(new InsetOptArg(buf));
571 } else if (tmptok == "Float") {
573 string tmptok = lex.getString();
574 inset.reset(new InsetFloat(buf, tmptok));
575 } else if (tmptok == "Wrap") {
577 string tmptok = lex.getString();
578 inset.reset(new InsetWrap(buf, tmptok));
579 } else if (tmptok == "Caption") {
580 inset.reset(new InsetCaption(buf));
581 } else if (tmptok == "Index") {
582 inset.reset(new InsetIndex(buf, InsetIndexParams()));
583 } else if (tmptok == "FloatList") {
584 inset.reset(new InsetFloatList);
585 } else if (tmptok == "Info") {
586 inset.reset(new InsetInfo(buf));
588 lyxerr << "unknown Inset type '" << tmptok
590 while (lex.isOK() && lex.getString() != "\\end_inset")
595 // Set the buffer reference for proper parsing of some insets
596 // (InsetCollapsable for example)
597 inset->setBuffer(const_cast<Buffer &>(buf));
599 // Set again the buffer for insets that are created inside this inset
600 // (InsetMathHull for example).
601 inset->setBuffer(const_cast<Buffer &>(buf));
603 return inset.release();