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 type = cmd.getArg(0);
154 //FIXME: only the float type (the first argument) is transmitted
155 // because of the InsetFloat ctor.
156 if (params.documentClass().floats().typeExist(type))
157 return new InsetFloat(buf, type);
158 lyxerr << "Non-existent float type: " << type << endl;
162 case LFUN_FLOAT_WIDE_INSERT: {
163 // check if the float type exists
164 string const argument = to_utf8(cmd.argument());
165 if (params.documentClass().floats().typeExist(argument)) {
166 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
170 lyxerr << "Non-existent float type: " << argument << endl;
174 case LFUN_WRAP_INSERT: {
175 string const argument = to_utf8(cmd.argument());
176 if (argument == "figure" || argument == "table")
177 return new InsetWrap(buf, argument);
178 lyxerr << "Non-existent wrapfig type: " << argument << endl;
182 case LFUN_INDEX_INSERT: {
183 docstring arg = cmd.argument();
184 return new InsetIndex(buf, InsetIndexParams(arg));
187 case LFUN_NOMENCL_INSERT: {
188 InsetCommandParams icp(NOMENCL_CODE);
189 icp["symbol"] = cmd.argument();
190 return new InsetNomencl(buf, icp);
193 case LFUN_TABULAR_INSERT: {
194 if (cmd.argument().empty())
196 istringstream ss(to_utf8(cmd.argument()));
203 return new InsetTabular(buf, r, c);
206 case LFUN_CAPTION_INSERT:
207 return new InsetCaption(buf);
209 case LFUN_INDEX_PRINT: {
210 InsetCommandParams icp(INDEX_PRINT_CODE);
211 icp["type"] = cmd.argument();
212 return new InsetPrintIndex(buf, icp);
215 case LFUN_NOMENCL_PRINT: {
216 InsetCommandParams icp(NOMENCL_PRINT_CODE);
217 icp["set_width"] = from_ascii("auto");
218 return new InsetPrintNomencl(buf, icp);
221 case LFUN_TOC_INSERT:
222 return new InsetTOC(buf, InsetCommandParams(TOC_CODE));
224 case LFUN_INFO_INSERT: {
225 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
230 case LFUN_INSET_INSERT: {
231 string const name = cmd.getArg(0);
232 InsetCode code = insetCode(name);
235 lyxerr << "No such inset '" << name << "'.";
239 InsetCommandParams icp(code);
240 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
241 return new InsetBibitem(buf, icp);
245 InsetCommandParams icp(code);
246 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
247 return new InsetBibtex(buf, icp);
251 InsetCommandParams icp(code);
252 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
253 return new InsetCitation(buf, icp);
257 return new InsetERT(buf,
258 InsetERT::string2params(to_utf8(cmd.argument())));
261 case LISTINGS_CODE: {
262 InsetListingsParams par;
263 InsetListings::string2params(to_utf8(cmd.argument()), par);
264 return new InsetListings(buf, par);
267 case EXTERNAL_CODE: {
268 InsetExternalParams iep;
269 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
270 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
271 inset->setBuffer(*buf);
272 inset->setParams(iep);
273 return inset.release();
276 case GRAPHICS_CODE: {
277 InsetGraphicsParams igp;
278 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
279 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
280 inset->setParams(igp);
281 return inset.release();
284 case HYPERLINK_CODE: {
285 InsetCommandParams icp(code);
286 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
287 return new InsetHyperlink(buf, icp);
291 InsetCommandParams icp(code);
292 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
293 return new InsetInclude(buf, icp);
297 docstring arg = cmd.argument();
298 return new InsetIndex(buf, InsetIndexParams(arg));
301 case INDEX_PRINT_CODE: {
302 InsetCommandParams icp(code);
303 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
304 return new InsetPrintIndex(buf, icp);
308 InsetCommandParams icp(code);
309 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
310 return new InsetNomencl(buf, icp);
314 InsetCommandParams icp(code);
315 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
316 return new InsetLabel(buf, icp);
320 InsetCommandParams icp(code);
321 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
322 return new InsetRef(buf, icp);
326 InsetSpaceParams isp;
327 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
328 return new InsetSpace(isp);
332 InsetCommandParams icp(code);
333 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
334 return new InsetTOC(buf, icp);
339 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
340 return new InsetVSpace(vspace);
344 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
349 } //end LFUN_INSET_INSERT
351 case LFUN_SPACE_INSERT: {
352 string const name = cmd.getArg(0);
353 string const len = cmd.getArg(1);
355 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
358 InsetSpaceParams isp;
359 // The tests for isp.math might be disabled after a file format change
360 if (name == "normal")
361 isp.kind = InsetSpaceParams::NORMAL;
362 else if (name == "protected")
363 isp.kind = InsetSpaceParams::PROTECTED;
364 else if (name == "thin")
365 isp.kind = InsetSpaceParams::THIN;
366 else if (isp.math && name == "med")
367 isp.kind = InsetSpaceParams::MEDIUM;
368 else if (isp.math && name == "thick")
369 isp.kind = InsetSpaceParams::THICK;
370 else if (name == "quad")
371 isp.kind = InsetSpaceParams::QUAD;
372 else if (name == "qquad")
373 isp.kind = InsetSpaceParams::QQUAD;
374 else if (name == "enspace")
375 isp.kind = InsetSpaceParams::ENSPACE;
376 else if (name == "enskip")
377 isp.kind = InsetSpaceParams::ENSKIP;
378 else if (name == "negthinspace")
379 isp.kind = InsetSpaceParams::NEGTHIN;
380 else if (isp.math && name == "negmedspace")
381 isp.kind = InsetSpaceParams::NEGMEDIUM;
382 else if (isp.math && name == "negthickspace")
383 isp.kind = InsetSpaceParams::NEGTHICK;
384 else if (name == "hfill")
385 isp.kind = InsetSpaceParams::HFILL;
386 else if (name == "hfill*")
387 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
388 else if (name == "dotfill")
389 isp.kind = InsetSpaceParams::DOTFILL;
390 else if (name == "hrulefill")
391 isp.kind = InsetSpaceParams::HRULEFILL;
392 else if (name == "hspace") {
393 if (len.empty() || !isValidGlueLength(len)) {
394 lyxerr << "LyX function 'space-insert hspace' "
395 << "needs a valid length argument." << endl;
398 isp.kind = InsetSpaceParams::CUSTOM;
399 isp.length = GlueLength(len);
401 else if (name == "hspace*") {
402 if (len.empty() || !isValidGlueLength(len)) {
403 lyxerr << "LyX function 'space-insert hspace*' "
404 << "needs a valid length argument." << endl;
407 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
408 isp.length = GlueLength(len);
411 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
414 return new InsetSpace(isp);
422 } catch (ExceptionMessage const & message) {
423 if (message.type_ == ErrorException) {
424 // This should never happen!
425 Alert::error(message.title_, message.details_);
427 } else if (message.type_ == WarningException) {
428 Alert::warning(message.title_, message.details_);
437 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
439 Inset * inset = createInsetHelper(buf, cmd);
441 inset->setBuffer(*buf);
446 Inset * readInset(Lexer & lex, Buffer * buf)
449 if (lex.getString() != "\\begin_inset")
450 LYXERR0("Buffer::readInset: Consistency check failed.");
452 auto_ptr<Inset> inset;
457 // test the different insets
459 // FIXME It would be better if we did not have this branch and could
460 // just do one massive switch for all insets. But at present, it's
461 // easier to do it this way, and we can't do the massive switch until
462 // the conversion mentioned below. Note that if we do want to do a
463 // single switch, we need to remove this "CommandInset" line---or
464 // replace it with a single "InsetType" line that would be used in all
466 if (tmptok == "CommandInset") {
468 string const insetType = lex.getString();
469 lex.pushToken(insetType);
471 InsetCode const code = insetCode(insetType);
473 //FIXME If we do the one massive switch, we cannot do this here, since
474 //we do not know in advance that we're dealing with a command inset.
475 //Worst case, we could put it in each case below. Better, we could
476 //pass the lexer to the constructor and let the params be built there.
477 InsetCommandParams inscmd(code);
482 inset.reset(new InsetBibitem(buf, inscmd));
485 inset.reset(new InsetBibtex(buf, inscmd));
488 inset.reset(new InsetCitation(buf, inscmd));
491 inset.reset(new InsetHyperlink(buf, inscmd));
494 inset.reset(new InsetInclude(buf, inscmd));
496 case INDEX_PRINT_CODE:
497 inset.reset(new InsetPrintIndex(buf, inscmd));
500 inset.reset(new InsetLabel(buf, inscmd));
503 inset.reset(new InsetNomencl(buf, inscmd));
505 case NOMENCL_PRINT_CODE:
506 inset.reset(new InsetPrintNomencl(buf, inscmd));
509 if (inscmd["name"].empty() && inscmd["reference"].empty())
511 inset.reset(new InsetRef(buf, inscmd));
514 inset.reset(new InsetTOC(buf, inscmd));
518 lyxerr << "unknown CommandInset '" << insetType
520 while (lex.isOK() && lex.getString() != "\\end_inset")
524 inset->setBuffer(*buf);
526 // FIXME This branch should be made to use inset codes as the preceding
527 // branch does. Unfortunately, that will take some doing. It requires
528 // converting the representation of the insets in LyX files so that they
529 // use the inset names listed in Inset.cpp. Then, as above, the inset names
530 // can be translated to inset codes using insetCode(). And the insets'
531 // write() routines should use insetName() rather than hardcoding it.
532 if (tmptok == "Quotes") {
533 inset.reset(new InsetQuotes(buf));
534 } else if (tmptok == "External") {
535 inset.reset(new InsetExternal(buf));
536 } else if (tmptok == "FormulaMacro") {
537 inset.reset(new MathMacroTemplate(buf));
538 } else if (tmptok == "Formula") {
539 inset.reset(new InsetMathHull(buf));
540 } else if (tmptok == "Graphics") {
541 inset.reset(new InsetGraphics(buf));
542 } else if (tmptok == "Note") {
543 inset.reset(new InsetNote(buf, tmptok));
544 } else if (tmptok == "Box") {
545 inset.reset(new InsetBox(buf, tmptok));
546 } else if (tmptok == "Flex") {
548 string s = lex.getString();
549 inset.reset(new InsetFlex(buf, s));
550 } else if (tmptok == "Branch") {
551 inset.reset(new InsetBranch(buf, InsetBranchParams()));
552 } else if (tmptok == "Phantom") {
553 inset.reset(new InsetPhantom(buf, tmptok));
554 } else if (tmptok == "ERT") {
555 inset.reset(new InsetERT(buf));
556 } else if (tmptok == "listings") {
557 inset.reset(new InsetListings(buf));
558 } else if (tmptok == "space") {
559 inset.reset(new InsetSpace);
560 } else if (tmptok == "Tabular") {
561 inset.reset(new InsetTabular(buf));
562 } else if (tmptok == "Text") {
563 inset.reset(new InsetText(buf));
564 } else if (tmptok == "VSpace") {
565 inset.reset(new InsetVSpace);
566 } else if (tmptok == "Foot") {
567 inset.reset(new InsetFoot(buf));
568 } else if (tmptok == "Marginal") {
569 inset.reset(new InsetMarginal(buf));
570 } else if (tmptok == "Newpage") {
571 inset.reset(new InsetNewpage);
572 } else if (tmptok == "Newline") {
573 inset.reset(new InsetNewline);
574 } else if (tmptok == "OptArg") {
575 inset.reset(new InsetOptArg(buf));
576 } else if (tmptok == "Float") {
578 string tmptok = lex.getString();
579 inset.reset(new InsetFloat(buf, tmptok));
580 } else if (tmptok == "Wrap") {
582 string tmptok = lex.getString();
583 inset.reset(new InsetWrap(buf, tmptok));
584 } else if (tmptok == "Caption") {
585 inset.reset(new InsetCaption(buf));
586 } else if (tmptok == "Index") {
587 inset.reset(new InsetIndex(buf, InsetIndexParams()));
588 } else if (tmptok == "FloatList") {
589 inset.reset(new InsetFloatList(buf));
590 } else if (tmptok == "Info") {
591 inset.reset(new InsetInfo(buf));
593 lyxerr << "unknown Inset type '" << tmptok
595 while (lex.isOK() && lex.getString() != "\\end_inset")
600 // Set the buffer reference for proper parsing of some insets
601 // (InsetCollapsable for example)
602 inset->setBuffer(*buf);
604 // Set again the buffer for insets that are created inside this inset
605 // (InsetMathHull for example).
606 inset->setBuffer(*buf);
608 return inset.release();