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/InsetOptArg.h"
48 #include "insets/InsetNewpage.h"
49 #include "insets/InsetRef.h"
50 #include "insets/InsetSpace.h"
51 #include "insets/InsetTabular.h"
52 #include "insets/InsetTOC.h"
53 #include "insets/InsetHyperlink.h"
54 #include "insets/InsetVSpace.h"
55 #include "insets/InsetWrap.h"
57 #include "mathed/MathMacroTemplate.h"
58 #include "mathed/InsetMathHull.h"
60 #include "frontends/alert.h"
62 #include "support/debug.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
66 #include "support/lassert.h"
71 using namespace lyx::support;
75 namespace Alert = frontend::Alert;
78 Inset * createInsetHelper(Buffer & buf, FuncRequest const & cmd)
80 BufferParams const & params = buf.params();
86 case LFUN_LINE_INSERT:
89 case LFUN_NEWPAGE_INSERT: {
90 string const name = cmd.getArg(0);
91 InsetNewpageParams inp;
92 if (name.empty() || name == "newpage")
93 inp.kind = InsetNewpageParams::NEWPAGE;
94 else if (name == "pagebreak")
95 inp.kind = InsetNewpageParams::PAGEBREAK;
96 else if (name == "clearpage")
97 inp.kind = InsetNewpageParams::CLEARPAGE;
98 else if (name == "cleardoublepage")
99 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
100 return new InsetNewpage(inp);
103 case LFUN_FLEX_INSERT: {
104 string s = cmd.getArg(0);
105 return new InsetFlex(buf, s);
108 case LFUN_NOTE_INSERT: {
109 string arg = cmd.getArg(0);
112 return new InsetNote(buf, arg);
115 case LFUN_BOX_INSERT: {
116 string arg = cmd.getArg(0);
119 return new InsetBox(buf, arg);
122 case LFUN_BRANCH_INSERT: {
123 docstring arg = cmd.argument();
125 arg = from_ascii("none");
126 return new InsetBranch(buf, InsetBranchParams(arg));
129 case LFUN_ERT_INSERT:
130 return new InsetERT(buf);
132 case LFUN_LISTING_INSERT:
133 return new InsetListings(buf);
135 case LFUN_FOOTNOTE_INSERT:
136 return new InsetFoot(buf);
138 case LFUN_MARGINALNOTE_INSERT:
139 return new InsetMarginal(buf);
141 case LFUN_OPTIONAL_INSERT:
142 return new InsetOptArg(buf);
144 case LFUN_BIBITEM_INSERT:
145 return new InsetBibitem(buf, InsetCommandParams(BIBITEM_CODE));
147 case LFUN_FLOAT_INSERT: {
148 // check if the float type exists
149 string const argument = to_utf8(cmd.argument());
150 if (buf.params().documentClass().floats().typeExist(argument))
151 return new InsetFloat(buf, argument);
152 lyxerr << "Non-existent float type: " << argument << endl;
155 case LFUN_FLOAT_WIDE_INSERT: {
156 // check if the float type exists
157 string const argument = to_utf8(cmd.argument());
158 if (params.documentClass().floats().typeExist(argument)) {
159 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
160 p->setWide(true, params);
163 lyxerr << "Non-existent float type: " << argument << endl;
167 case LFUN_WRAP_INSERT: {
168 string const argument = to_utf8(cmd.argument());
169 if (argument == "figure" || argument == "table")
170 return new InsetWrap(buf, argument);
171 lyxerr << "Non-existent wrapfig type: " << argument << endl;
175 case LFUN_INDEX_INSERT:
176 return new InsetIndex(buf);
178 case LFUN_NOMENCL_INSERT: {
179 InsetCommandParams icp(NOMENCL_CODE);
180 icp["symbol"] = cmd.argument();
181 return new InsetNomencl(icp);
184 case LFUN_TABULAR_INSERT: {
185 if (cmd.argument().empty())
187 istringstream ss(to_utf8(cmd.argument()));
194 return new InsetTabular(buf, r, c);
197 case LFUN_CAPTION_INSERT:
198 return new InsetCaption(buf);
200 case LFUN_INDEX_PRINT:
201 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
203 case LFUN_NOMENCL_PRINT:
204 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
206 case LFUN_TOC_INSERT:
207 return new InsetTOC(InsetCommandParams(TOC_CODE));
209 case LFUN_INFO_INSERT: {
210 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
215 case LFUN_INSET_INSERT: {
216 string const name = cmd.getArg(0);
217 InsetCode code = insetCode(name);
220 lyxerr << "No such inset '" << name << "'.";
224 InsetCommandParams icp(code);
225 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
226 return new InsetBibitem(buf, icp);
230 InsetCommandParams icp(code);
231 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
232 return new InsetBibtex(buf, icp);
236 InsetCommandParams icp(code);
237 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
238 return new InsetCitation(icp);
242 return new InsetERT(buf,
243 InsetERT::string2params(to_utf8(cmd.argument())));
246 case LISTINGS_CODE: {
247 InsetListingsParams par;
248 InsetListings::string2params(to_utf8(cmd.argument()), par);
249 return new InsetListings(buf, par);
252 case EXTERNAL_CODE: {
253 InsetExternalParams iep;
254 InsetExternal::string2params(to_utf8(cmd.argument()), buf, iep);
255 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
256 inset->setBuffer(buf);
257 inset->setParams(iep);
258 return inset.release();
261 case GRAPHICS_CODE: {
262 InsetGraphicsParams igp;
263 InsetGraphics::string2params(to_utf8(cmd.argument()), buf, igp);
264 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
265 inset->setParams(igp);
266 return inset.release();
269 case HYPERLINK_CODE: {
270 InsetCommandParams icp(code);
271 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
272 return new InsetHyperlink(icp);
276 InsetCommandParams icp(code);
277 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
278 return new InsetInclude(icp);
282 return new InsetIndex(buf);
285 InsetCommandParams icp(code);
286 InsetCommand::string2params(name, lyx::to_utf8(cmd.argument()), icp);
287 return new InsetNomencl(icp);
291 InsetCommandParams icp(code);
292 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
293 return new InsetLabel(icp);
297 InsetCommandParams icp(code);
298 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
299 return new InsetRef(buf, icp);
303 InsetSpaceParams isp;
304 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
305 return new InsetSpace(isp);
309 InsetCommandParams icp(code);
310 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
311 return new InsetTOC(icp);
316 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
317 return new InsetVSpace(vspace);
321 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
326 } //end LFUN_INSET_INSERT
328 case LFUN_SPACE_INSERT: {
329 string const name = cmd.getArg(0);
330 string const len = cmd.getArg(1);
332 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
335 InsetSpaceParams isp;
336 if (name == "normal")
337 isp.kind = InsetSpaceParams::NORMAL;
338 else if (name == "protected")
339 isp.kind = InsetSpaceParams::PROTECTED;
340 else if (name == "thin")
341 isp.kind = InsetSpaceParams::THIN;
342 else if (name == "quad")
343 isp.kind = InsetSpaceParams::QUAD;
344 else if (name == "qquad")
345 isp.kind = InsetSpaceParams::QQUAD;
346 else if (name == "enspace")
347 isp.kind = InsetSpaceParams::ENSPACE;
348 else if (name == "enskip")
349 isp.kind = InsetSpaceParams::ENSKIP;
350 else if (name == "negthinspace")
351 isp.kind = InsetSpaceParams::NEGTHIN;
352 else if (name == "hfill")
353 isp.kind = InsetSpaceParams::HFILL;
354 else if (name == "hfill*")
355 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
356 else if (name == "dotfill")
357 isp.kind = InsetSpaceParams::DOTFILL;
358 else if (name == "hrulefill")
359 isp.kind = InsetSpaceParams::HRULEFILL;
360 else if (name == "hspace") {
361 if (len.empty() || !isValidLength(len)) {
362 lyxerr << "LyX function 'space-insert hspace' "
363 << "needs a valid length argument." << endl;
366 isp.kind = InsetSpaceParams::CUSTOM;
367 isp.length = Length(len);
369 else if (name == "hspace*") {
370 if (len.empty() || !isValidLength(len)) {
371 lyxerr << "LyX function 'space-insert hspace*' "
372 << "needs a valid length argument." << endl;
375 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
376 isp.length = Length(len);
379 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
382 return new InsetSpace(isp);
390 } catch (ExceptionMessage const & message) {
391 if (message.type_ == ErrorException) {
392 // This should never happen!
393 Alert::error(message.title_, message.details_);
395 } else if (message.type_ == WarningException) {
396 Alert::warning(message.title_, message.details_);
405 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
407 Inset * inset = createInsetHelper(buf, cmd);
409 inset->setBuffer(buf);
414 Inset * readInset(Lexer & lex, Buffer const & buf)
417 if (lex.getString() != "\\begin_inset")
418 LYXERR0("Buffer::readInset: Consistency check failed.");
420 auto_ptr<Inset> inset;
425 // test the different insets
427 // FIXME It would be better if we did not have this branch and could
428 // just do one massive switch for all insets. But at present, it's
429 // easier to do it this way, and we can't do the massive switch until
430 // the conversion mentioned below. Note that if we do want to do a
431 // single switch, we need to remove this "CommandInset" line---or
432 // replace it with a single "InsetType" line that would be used in all
434 if (tmptok == "CommandInset") {
436 string const insetType = lex.getString();
437 lex.pushToken(insetType);
439 InsetCode const code = insetCode(insetType);
441 //FIXME If we do the one massive switch, we cannot do this here, since
442 //we do not know in advance that we're dealing with a command inset.
443 //Worst case, we could put it in each case below. Better, we could
444 //pass the lexer to the constructor and let the params be built there.
445 InsetCommandParams inscmd(code);
450 inset.reset(new InsetBibitem(buf, inscmd));
453 inset.reset(new InsetBibtex(buf, inscmd));
456 inset.reset(new InsetCitation(inscmd));
459 inset.reset(new InsetHyperlink(inscmd));
462 inset.reset(new InsetInclude(inscmd));
464 case INDEX_PRINT_CODE:
465 inset.reset(new InsetPrintIndex(inscmd));
468 inset.reset(new InsetLabel(inscmd));
471 inset.reset(new InsetNomencl(inscmd));
473 case NOMENCL_PRINT_CODE:
474 inset.reset(new InsetPrintNomencl(inscmd));
477 if (inscmd["name"].empty() && inscmd["reference"].empty())
479 inset.reset(new InsetRef(buf, inscmd));
482 inset.reset(new InsetTOC(inscmd));
486 lyxerr << "unknown CommandInset '" << insetType
488 while (lex.isOK() && lex.getString() != "\\end_inset")
492 inset->setBuffer(const_cast<Buffer &>(buf));
494 // FIXME This branch should be made to use inset codes as the preceding
495 // branch does. Unfortunately, that will take some doing. It requires
496 // converting the representation of the insets in LyX files so that they
497 // use the inset names listed in Inset.cpp. Then, as above, the inset names
498 // can be translated to inset codes using insetCode(). And the insets'
499 // write() routines should use insetName() rather than hardcoding it.
500 if (tmptok == "Quotes") {
501 inset.reset(new InsetQuotes);
502 } else if (tmptok == "External") {
503 inset.reset(new InsetExternal(const_cast<Buffer &>(buf)));
504 } else if (tmptok == "FormulaMacro") {
505 inset.reset(new MathMacroTemplate);
506 } else if (tmptok == "Formula") {
507 inset.reset(new InsetMathHull);
508 } else if (tmptok == "Graphics") {
509 inset.reset(new InsetGraphics(const_cast<Buffer &>(buf)));
510 } else if (tmptok == "Note") {
511 inset.reset(new InsetNote(buf, tmptok));
512 } else if (tmptok == "Box") {
513 inset.reset(new InsetBox(buf, tmptok));
514 } else if (tmptok == "Flex") {
516 string s = lex.getString();
517 inset.reset(new InsetFlex(buf, s));
518 } else if (tmptok == "Branch") {
519 inset.reset(new InsetBranch(buf, InsetBranchParams()));
520 } else if (tmptok == "ERT") {
521 inset.reset(new InsetERT(buf));
522 } else if (tmptok == "listings") {
523 inset.reset(new InsetListings(buf));
524 } else if (tmptok == "space") {
525 inset.reset(new InsetSpace);
526 } else if (tmptok == "Tabular") {
527 inset.reset(new InsetTabular(buf));
528 } else if (tmptok == "Text") {
529 inset.reset(new InsetText(buf));
530 } else if (tmptok == "VSpace") {
531 inset.reset(new InsetVSpace);
532 } else if (tmptok == "Foot") {
533 inset.reset(new InsetFoot(buf));
534 } else if (tmptok == "Marginal") {
535 inset.reset(new InsetMarginal(buf));
536 } else if (tmptok == "Newpage") {
537 inset.reset(new InsetNewpage);
538 } else if (tmptok == "Newline") {
539 inset.reset(new InsetNewline);
540 } else if (tmptok == "OptArg") {
541 inset.reset(new InsetOptArg(buf));
542 } else if (tmptok == "Float") {
544 string tmptok = lex.getString();
545 inset.reset(new InsetFloat(buf, tmptok));
546 } else if (tmptok == "Wrap") {
548 string tmptok = lex.getString();
549 inset.reset(new InsetWrap(buf, tmptok));
550 } else if (tmptok == "Caption") {
551 inset.reset(new InsetCaption(buf));
552 } else if (tmptok == "Index") {
553 inset.reset(new InsetIndex(buf));
554 } else if (tmptok == "FloatList") {
555 inset.reset(new InsetFloatList);
556 } else if (tmptok == "Info") {
557 inset.reset(new InsetInfo(buf));
559 lyxerr << "unknown Inset type '" << tmptok
561 while (lex.isOK() && lex.getString() != "\\end_inset")
566 // Set the buffer reference for proper parsing of some insets
567 // (InsetCollapsable for example)
568 inset->setBuffer(const_cast<Buffer &>(buf));
570 // Set again the buffer for insets that are created inside this inset
571 // (InsetMathHull for example).
572 inset->setBuffer(const_cast<Buffer &>(buf));
574 return inset.release();