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/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.h"
44 #include "insets/InsetNewline.h"
45 #include "insets/InsetNewpage.h"
46 #include "insets/InsetNomencl.h"
47 #include "insets/InsetNote.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetPhantom.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.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_PHANTOM_INSERT: {
130 string arg = cmd.getArg(0);
133 return new InsetPhantom(buf, arg);
136 case LFUN_ERT_INSERT:
137 return new InsetERT(buf);
139 case LFUN_LISTING_INSERT:
140 return new InsetListings(buf);
142 case LFUN_FOOTNOTE_INSERT:
143 return new InsetFoot(buf);
145 case LFUN_MARGINALNOTE_INSERT:
146 return new InsetMarginal(buf);
148 case LFUN_OPTIONAL_INSERT:
149 return new InsetOptArg(buf);
151 case LFUN_FLOAT_INSERT: {
152 // check if the float type exists
153 string const argument = to_utf8(cmd.argument());
154 if (buf.params().documentClass().floats().typeExist(argument))
155 return new InsetFloat(buf, argument);
156 lyxerr << "Non-existent float type: " << argument << endl;
159 case LFUN_FLOAT_WIDE_INSERT: {
160 // check if the float type exists
161 string const argument = to_utf8(cmd.argument());
162 if (params.documentClass().floats().typeExist(argument)) {
163 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
164 p->setWide(true, params);
167 lyxerr << "Non-existent float type: " << argument << endl;
171 case LFUN_WRAP_INSERT: {
172 string const argument = to_utf8(cmd.argument());
173 if (argument == "figure" || argument == "table")
174 return new InsetWrap(buf, argument);
175 lyxerr << "Non-existent wrapfig type: " << argument << endl;
179 case LFUN_INDEX_INSERT: {
180 docstring arg = cmd.argument();
181 return new InsetIndex(buf, InsetIndexParams(arg));
184 case LFUN_NOMENCL_INSERT: {
185 InsetCommandParams icp(NOMENCL_CODE);
186 icp["symbol"] = cmd.argument();
187 return new InsetNomencl(icp);
190 case LFUN_TABULAR_INSERT: {
191 if (cmd.argument().empty())
193 istringstream ss(to_utf8(cmd.argument()));
200 return new InsetTabular(buf, r, c);
203 case LFUN_CAPTION_INSERT:
204 return new InsetCaption(buf);
206 case LFUN_INDEX_PRINT: {
207 InsetCommandParams icp(INDEX_PRINT_CODE);
208 icp["type"] = cmd.argument();
209 return new InsetPrintIndex(icp);
212 case LFUN_NOMENCL_PRINT:
213 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
215 case LFUN_TOC_INSERT:
216 return new InsetTOC(InsetCommandParams(TOC_CODE));
218 case LFUN_INFO_INSERT: {
219 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
224 case LFUN_INSET_INSERT: {
225 string const name = cmd.getArg(0);
226 InsetCode code = insetCode(name);
229 lyxerr << "No such inset '" << name << "'.";
233 InsetCommandParams icp(code);
234 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
235 return new InsetBibitem(buf, icp);
239 InsetCommandParams icp(code);
240 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
241 return new InsetBibtex(buf, icp);
245 InsetCommandParams icp(code);
246 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
247 return new InsetCitation(icp);
251 return new InsetERT(buf,
252 InsetERT::string2params(to_utf8(cmd.argument())));
255 case LISTINGS_CODE: {
256 InsetListingsParams par;
257 InsetListings::string2params(to_utf8(cmd.argument()), par);
258 return new InsetListings(buf, par);
261 case EXTERNAL_CODE: {
262 InsetExternalParams iep;
263 InsetExternal::string2params(to_utf8(cmd.argument()), buf, iep);
264 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
265 inset->setBuffer(buf);
266 inset->setParams(iep);
267 return inset.release();
270 case GRAPHICS_CODE: {
271 InsetGraphicsParams igp;
272 InsetGraphics::string2params(to_utf8(cmd.argument()), buf, igp);
273 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
274 inset->setParams(igp);
275 return inset.release();
278 case HYPERLINK_CODE: {
279 InsetCommandParams icp(code);
280 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
281 return new InsetHyperlink(icp);
285 InsetCommandParams icp(code);
286 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
287 return new InsetInclude(icp);
291 docstring arg = cmd.argument();
292 return new InsetIndex(buf, InsetIndexParams(arg));
295 case INDEX_PRINT_CODE: {
296 InsetCommandParams icp(code);
297 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
298 return new InsetPrintIndex(icp);
302 InsetCommandParams icp(code);
303 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
304 return new InsetNomencl(icp);
308 InsetCommandParams icp(code);
309 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
310 return new InsetLabel(icp);
314 InsetCommandParams icp(code);
315 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
316 return new InsetRef(buf, icp);
320 InsetSpaceParams isp;
321 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
322 return new InsetSpace(isp);
326 InsetCommandParams icp(code);
327 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
328 return new InsetTOC(icp);
333 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
334 return new InsetVSpace(vspace);
338 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
343 } //end LFUN_INSET_INSERT
345 case LFUN_SPACE_INSERT: {
346 string const name = cmd.getArg(0);
347 string const len = cmd.getArg(1);
349 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
352 InsetSpaceParams isp;
353 // The tests for isp.math might be disabled after a file format change
354 if (name == "normal")
355 isp.kind = InsetSpaceParams::NORMAL;
356 else if (name == "protected")
357 isp.kind = InsetSpaceParams::PROTECTED;
358 else if (name == "thin")
359 isp.kind = InsetSpaceParams::THIN;
360 else if (isp.math && name == "med")
361 isp.kind = InsetSpaceParams::MEDIUM;
362 else if (isp.math && name == "thick")
363 isp.kind = InsetSpaceParams::THICK;
364 else if (name == "quad")
365 isp.kind = InsetSpaceParams::QUAD;
366 else if (name == "qquad")
367 isp.kind = InsetSpaceParams::QQUAD;
368 else if (name == "enspace")
369 isp.kind = InsetSpaceParams::ENSPACE;
370 else if (name == "enskip")
371 isp.kind = InsetSpaceParams::ENSKIP;
372 else if (name == "negthinspace")
373 isp.kind = InsetSpaceParams::NEGTHIN;
374 else if (isp.math && name == "negmedspace")
375 isp.kind = InsetSpaceParams::NEGMEDIUM;
376 else if (isp.math && name == "negthickspace")
377 isp.kind = InsetSpaceParams::NEGTHICK;
378 else if (name == "hfill")
379 isp.kind = InsetSpaceParams::HFILL;
380 else if (name == "hfill*")
381 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
382 else if (name == "dotfill")
383 isp.kind = InsetSpaceParams::DOTFILL;
384 else if (name == "hrulefill")
385 isp.kind = InsetSpaceParams::HRULEFILL;
386 else if (name == "hspace") {
387 if (len.empty() || !isValidLength(len)) {
388 lyxerr << "LyX function 'space-insert hspace' "
389 << "needs a valid length argument." << endl;
392 isp.kind = InsetSpaceParams::CUSTOM;
393 isp.length = Length(len);
395 else if (name == "hspace*") {
396 if (len.empty() || !isValidLength(len)) {
397 lyxerr << "LyX function 'space-insert hspace*' "
398 << "needs a valid length argument." << endl;
401 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
402 isp.length = Length(len);
405 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
408 return new InsetSpace(isp);
416 } catch (ExceptionMessage const & message) {
417 if (message.type_ == ErrorException) {
418 // This should never happen!
419 Alert::error(message.title_, message.details_);
421 } else if (message.type_ == WarningException) {
422 Alert::warning(message.title_, message.details_);
431 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
433 Inset * inset = createInsetHelper(buf, cmd);
435 inset->setBuffer(buf);
440 Inset * readInset(Lexer & lex, Buffer const & buf)
443 if (lex.getString() != "\\begin_inset")
444 LYXERR0("Buffer::readInset: Consistency check failed.");
446 auto_ptr<Inset> inset;
451 // test the different insets
453 // FIXME It would be better if we did not have this branch and could
454 // just do one massive switch for all insets. But at present, it's
455 // easier to do it this way, and we can't do the massive switch until
456 // the conversion mentioned below. Note that if we do want to do a
457 // single switch, we need to remove this "CommandInset" line---or
458 // replace it with a single "InsetType" line that would be used in all
460 if (tmptok == "CommandInset") {
462 string const insetType = lex.getString();
463 lex.pushToken(insetType);
465 InsetCode const code = insetCode(insetType);
467 //FIXME If we do the one massive switch, we cannot do this here, since
468 //we do not know in advance that we're dealing with a command inset.
469 //Worst case, we could put it in each case below. Better, we could
470 //pass the lexer to the constructor and let the params be built there.
471 InsetCommandParams inscmd(code);
476 inset.reset(new InsetBibitem(buf, inscmd));
479 inset.reset(new InsetBibtex(buf, inscmd));
482 inset.reset(new InsetCitation(inscmd));
485 inset.reset(new InsetHyperlink(inscmd));
488 inset.reset(new InsetInclude(inscmd));
490 case INDEX_PRINT_CODE:
491 inset.reset(new InsetPrintIndex(inscmd));
494 inset.reset(new InsetLabel(inscmd));
497 inset.reset(new InsetNomencl(inscmd));
499 case NOMENCL_PRINT_CODE:
500 inset.reset(new InsetPrintNomencl(inscmd));
503 if (inscmd["name"].empty() && inscmd["reference"].empty())
505 inset.reset(new InsetRef(buf, inscmd));
508 inset.reset(new InsetTOC(inscmd));
512 lyxerr << "unknown CommandInset '" << insetType
514 while (lex.isOK() && lex.getString() != "\\end_inset")
518 inset->setBuffer(const_cast<Buffer &>(buf));
520 // FIXME This branch should be made to use inset codes as the preceding
521 // branch does. Unfortunately, that will take some doing. It requires
522 // converting the representation of the insets in LyX files so that they
523 // use the inset names listed in Inset.cpp. Then, as above, the inset names
524 // can be translated to inset codes using insetCode(). And the insets'
525 // write() routines should use insetName() rather than hardcoding it.
526 if (tmptok == "Quotes") {
527 inset.reset(new InsetQuotes(buf));
528 } else if (tmptok == "External") {
529 inset.reset(new InsetExternal(const_cast<Buffer &>(buf)));
530 } else if (tmptok == "FormulaMacro") {
531 inset.reset(new MathMacroTemplate);
532 } else if (tmptok == "Formula") {
533 inset.reset(new InsetMathHull);
534 } else if (tmptok == "Graphics") {
535 inset.reset(new InsetGraphics(const_cast<Buffer &>(buf)));
536 } else if (tmptok == "Note") {
537 inset.reset(new InsetNote(buf, tmptok));
538 } else if (tmptok == "Box") {
539 inset.reset(new InsetBox(buf, tmptok));
540 } else if (tmptok == "Flex") {
542 string s = lex.getString();
543 inset.reset(new InsetFlex(buf, s));
544 } else if (tmptok == "Branch") {
545 inset.reset(new InsetBranch(buf, InsetBranchParams()));
546 } else if (tmptok == "Phantom") {
547 inset.reset(new InsetPhantom(buf, tmptok));
548 } else if (tmptok == "ERT") {
549 inset.reset(new InsetERT(buf));
550 } else if (tmptok == "listings") {
551 inset.reset(new InsetListings(buf));
552 } else if (tmptok == "space") {
553 inset.reset(new InsetSpace);
554 } else if (tmptok == "Tabular") {
555 inset.reset(new InsetTabular(const_cast<Buffer &>(buf)));
556 } else if (tmptok == "Text") {
557 inset.reset(new InsetText(buf));
558 } else if (tmptok == "VSpace") {
559 inset.reset(new InsetVSpace);
560 } else if (tmptok == "Foot") {
561 inset.reset(new InsetFoot(buf));
562 } else if (tmptok == "Marginal") {
563 inset.reset(new InsetMarginal(buf));
564 } else if (tmptok == "Newpage") {
565 inset.reset(new InsetNewpage);
566 } else if (tmptok == "Newline") {
567 inset.reset(new InsetNewline);
568 } else if (tmptok == "OptArg") {
569 inset.reset(new InsetOptArg(buf));
570 } else if (tmptok == "Float") {
572 string tmptok = lex.getString();
573 inset.reset(new InsetFloat(buf, tmptok));
574 } else if (tmptok == "Wrap") {
576 string tmptok = lex.getString();
577 inset.reset(new InsetWrap(buf, tmptok));
578 } else if (tmptok == "Caption") {
579 inset.reset(new InsetCaption(buf));
580 } else if (tmptok == "Index") {
581 inset.reset(new InsetIndex(buf, InsetIndexParams()));
582 } else if (tmptok == "FloatList") {
583 inset.reset(new InsetFloatList);
584 } else if (tmptok == "Info") {
585 inset.reset(new InsetInfo(buf));
587 lyxerr << "unknown Inset type '" << tmptok
589 while (lex.isOK() && lex.getString() != "\\end_inset")
594 // Set the buffer reference for proper parsing of some insets
595 // (InsetCollapsable for example)
596 inset->setBuffer(const_cast<Buffer &>(buf));
598 // Set again the buffer for insets that are created inside this inset
599 // (InsetMathHull for example).
600 inset->setBuffer(const_cast<Buffer &>(buf));
602 return inset.release();