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_FLOAT_INSERT: {
145 // check if the float type exists
146 string const argument = to_utf8(cmd.argument());
147 if (buf.params().documentClass().floats().typeExist(argument))
148 return new InsetFloat(buf, argument);
149 lyxerr << "Non-existent float type: " << argument << endl;
152 case LFUN_FLOAT_WIDE_INSERT: {
153 // check if the float type exists
154 string const argument = to_utf8(cmd.argument());
155 if (params.documentClass().floats().typeExist(argument)) {
156 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
157 p->setWide(true, params);
160 lyxerr << "Non-existent float type: " << argument << endl;
164 case LFUN_WRAP_INSERT: {
165 string const argument = to_utf8(cmd.argument());
166 if (argument == "figure" || argument == "table")
167 return new InsetWrap(buf, argument);
168 lyxerr << "Non-existent wrapfig type: " << argument << endl;
172 case LFUN_INDEX_INSERT:
173 return new InsetIndex(buf);
175 case LFUN_NOMENCL_INSERT: {
176 InsetCommandParams icp(NOMENCL_CODE);
177 icp["symbol"] = cmd.argument();
178 return new InsetNomencl(icp);
181 case LFUN_TABULAR_INSERT: {
182 if (cmd.argument().empty())
184 istringstream ss(to_utf8(cmd.argument()));
191 return new InsetTabular(buf, r, c);
194 case LFUN_CAPTION_INSERT:
195 return new InsetCaption(buf);
197 case LFUN_INDEX_PRINT:
198 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
200 case LFUN_NOMENCL_PRINT:
201 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
203 case LFUN_TOC_INSERT:
204 return new InsetTOC(InsetCommandParams(TOC_CODE));
206 case LFUN_INFO_INSERT: {
207 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
212 case LFUN_INSET_INSERT: {
213 string const name = cmd.getArg(0);
214 InsetCode code = insetCode(name);
217 lyxerr << "No such inset '" << name << "'.";
221 InsetCommandParams icp(code);
222 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
223 return new InsetBibitem(buf, icp);
227 InsetCommandParams icp(code);
228 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
229 return new InsetBibtex(buf, icp);
233 InsetCommandParams icp(code);
234 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
235 return new InsetCitation(icp);
239 return new InsetERT(buf,
240 InsetERT::string2params(to_utf8(cmd.argument())));
243 case LISTINGS_CODE: {
244 InsetListingsParams par;
245 InsetListings::string2params(to_utf8(cmd.argument()), par);
246 return new InsetListings(buf, par);
249 case EXTERNAL_CODE: {
250 InsetExternalParams iep;
251 InsetExternal::string2params(to_utf8(cmd.argument()), buf, iep);
252 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
253 inset->setBuffer(buf);
254 inset->setParams(iep);
255 return inset.release();
258 case GRAPHICS_CODE: {
259 InsetGraphicsParams igp;
260 InsetGraphics::string2params(to_utf8(cmd.argument()), buf, igp);
261 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
262 inset->setParams(igp);
263 return inset.release();
266 case HYPERLINK_CODE: {
267 InsetCommandParams icp(code);
268 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
269 return new InsetHyperlink(icp);
273 InsetCommandParams icp(code);
274 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
275 return new InsetInclude(icp);
279 return new InsetIndex(buf);
282 InsetCommandParams icp(code);
283 InsetCommand::string2params(name, lyx::to_utf8(cmd.argument()), icp);
284 return new InsetNomencl(icp);
288 InsetCommandParams icp(code);
289 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
290 return new InsetLabel(icp);
294 InsetCommandParams icp(code);
295 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
296 return new InsetRef(buf, icp);
300 InsetSpaceParams isp;
301 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
302 return new InsetSpace(isp);
306 InsetCommandParams icp(code);
307 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
308 return new InsetTOC(icp);
313 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
314 return new InsetVSpace(vspace);
318 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
323 } //end LFUN_INSET_INSERT
325 case LFUN_SPACE_INSERT: {
326 string const name = cmd.getArg(0);
327 string const len = cmd.getArg(1);
329 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
332 InsetSpaceParams isp;
333 if (name == "normal")
334 isp.kind = InsetSpaceParams::NORMAL;
335 else if (name == "protected")
336 isp.kind = InsetSpaceParams::PROTECTED;
337 else if (name == "thin")
338 isp.kind = InsetSpaceParams::THIN;
339 else if (name == "quad")
340 isp.kind = InsetSpaceParams::QUAD;
341 else if (name == "qquad")
342 isp.kind = InsetSpaceParams::QQUAD;
343 else if (name == "enspace")
344 isp.kind = InsetSpaceParams::ENSPACE;
345 else if (name == "enskip")
346 isp.kind = InsetSpaceParams::ENSKIP;
347 else if (name == "negthinspace")
348 isp.kind = InsetSpaceParams::NEGTHIN;
349 else if (name == "hfill")
350 isp.kind = InsetSpaceParams::HFILL;
351 else if (name == "hfill*")
352 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
353 else if (name == "dotfill")
354 isp.kind = InsetSpaceParams::DOTFILL;
355 else if (name == "hrulefill")
356 isp.kind = InsetSpaceParams::HRULEFILL;
357 else if (name == "hspace") {
358 if (len.empty() || !isValidLength(len)) {
359 lyxerr << "LyX function 'space-insert hspace' "
360 << "needs a valid length argument." << endl;
363 isp.kind = InsetSpaceParams::CUSTOM;
364 isp.length = Length(len);
366 else if (name == "hspace*") {
367 if (len.empty() || !isValidLength(len)) {
368 lyxerr << "LyX function 'space-insert hspace*' "
369 << "needs a valid length argument." << endl;
372 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
373 isp.length = Length(len);
376 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
379 return new InsetSpace(isp);
387 } catch (ExceptionMessage const & message) {
388 if (message.type_ == ErrorException) {
389 // This should never happen!
390 Alert::error(message.title_, message.details_);
392 } else if (message.type_ == WarningException) {
393 Alert::warning(message.title_, message.details_);
402 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
404 Inset * inset = createInsetHelper(buf, cmd);
406 inset->setBuffer(buf);
411 Inset * readInset(Lexer & lex, Buffer const & buf)
414 if (lex.getString() != "\\begin_inset")
415 LYXERR0("Buffer::readInset: Consistency check failed.");
417 auto_ptr<Inset> inset;
422 // test the different insets
424 // FIXME It would be better if we did not have this branch and could
425 // just do one massive switch for all insets. But at present, it's
426 // easier to do it this way, and we can't do the massive switch until
427 // the conversion mentioned below. Note that if we do want to do a
428 // single switch, we need to remove this "CommandInset" line---or
429 // replace it with a single "InsetType" line that would be used in all
431 if (tmptok == "CommandInset") {
433 string const insetType = lex.getString();
434 lex.pushToken(insetType);
436 InsetCode const code = insetCode(insetType);
438 //FIXME If we do the one massive switch, we cannot do this here, since
439 //we do not know in advance that we're dealing with a command inset.
440 //Worst case, we could put it in each case below. Better, we could
441 //pass the lexer to the constructor and let the params be built there.
442 InsetCommandParams inscmd(code);
447 inset.reset(new InsetBibitem(buf, inscmd));
450 inset.reset(new InsetBibtex(buf, inscmd));
453 inset.reset(new InsetCitation(inscmd));
456 inset.reset(new InsetHyperlink(inscmd));
459 inset.reset(new InsetInclude(inscmd));
461 case INDEX_PRINT_CODE:
462 inset.reset(new InsetPrintIndex(inscmd));
465 inset.reset(new InsetLabel(inscmd));
468 inset.reset(new InsetNomencl(inscmd));
470 case NOMENCL_PRINT_CODE:
471 inset.reset(new InsetPrintNomencl(inscmd));
474 if (inscmd["name"].empty() && inscmd["reference"].empty())
476 inset.reset(new InsetRef(buf, inscmd));
479 inset.reset(new InsetTOC(inscmd));
483 lyxerr << "unknown CommandInset '" << insetType
485 while (lex.isOK() && lex.getString() != "\\end_inset")
489 inset->setBuffer(const_cast<Buffer &>(buf));
491 // FIXME This branch should be made to use inset codes as the preceding
492 // branch does. Unfortunately, that will take some doing. It requires
493 // converting the representation of the insets in LyX files so that they
494 // use the inset names listed in Inset.cpp. Then, as above, the inset names
495 // can be translated to inset codes using insetCode(). And the insets'
496 // write() routines should use insetName() rather than hardcoding it.
497 if (tmptok == "Quotes") {
498 inset.reset(new InsetQuotes);
499 } else if (tmptok == "External") {
500 inset.reset(new InsetExternal(const_cast<Buffer &>(buf)));
501 } else if (tmptok == "FormulaMacro") {
502 inset.reset(new MathMacroTemplate);
503 } else if (tmptok == "Formula") {
504 inset.reset(new InsetMathHull);
505 } else if (tmptok == "Graphics") {
506 inset.reset(new InsetGraphics(const_cast<Buffer &>(buf)));
507 } else if (tmptok == "Note") {
508 inset.reset(new InsetNote(buf, tmptok));
509 } else if (tmptok == "Box") {
510 inset.reset(new InsetBox(buf, tmptok));
511 } else if (tmptok == "Flex") {
513 string s = lex.getString();
514 inset.reset(new InsetFlex(buf, s));
515 } else if (tmptok == "Branch") {
516 inset.reset(new InsetBranch(buf, InsetBranchParams()));
517 } else if (tmptok == "ERT") {
518 inset.reset(new InsetERT(buf));
519 } else if (tmptok == "listings") {
520 inset.reset(new InsetListings(buf));
521 } else if (tmptok == "space") {
522 inset.reset(new InsetSpace);
523 } else if (tmptok == "Tabular") {
524 inset.reset(new InsetTabular(const_cast<Buffer &>(buf)));
525 } else if (tmptok == "Text") {
526 inset.reset(new InsetText(buf));
527 } else if (tmptok == "VSpace") {
528 inset.reset(new InsetVSpace);
529 } else if (tmptok == "Foot") {
530 inset.reset(new InsetFoot(buf));
531 } else if (tmptok == "Marginal") {
532 inset.reset(new InsetMarginal(buf));
533 } else if (tmptok == "Newpage") {
534 inset.reset(new InsetNewpage);
535 } else if (tmptok == "Newline") {
536 inset.reset(new InsetNewline);
537 } else if (tmptok == "OptArg") {
538 inset.reset(new InsetOptArg(buf));
539 } else if (tmptok == "Float") {
541 string tmptok = lex.getString();
542 inset.reset(new InsetFloat(buf, tmptok));
543 } else if (tmptok == "Wrap") {
545 string tmptok = lex.getString();
546 inset.reset(new InsetWrap(buf, tmptok));
547 } else if (tmptok == "Caption") {
548 inset.reset(new InsetCaption(buf));
549 } else if (tmptok == "Index") {
550 inset.reset(new InsetIndex(buf));
551 } else if (tmptok == "FloatList") {
552 inset.reset(new InsetFloatList);
553 } else if (tmptok == "Info") {
554 inset.reset(new InsetInfo(buf));
556 lyxerr << "unknown Inset type '" << tmptok
558 while (lex.isOK() && lex.getString() != "\\end_inset")
563 // Set the buffer reference for proper parsing of some insets
564 // (InsetCollapsable for example)
565 inset->setBuffer(const_cast<Buffer &>(buf));
567 // Set again the buffer for insets that are created inside this inset
568 // (InsetMathHull for example).
569 inset->setBuffer(const_cast<Buffer &>(buf));
571 return inset.release();