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/InsetEnvironment.h"
29 #include "insets/InsetERT.h"
30 #include "insets/InsetListings.h"
31 #include "insets/InsetExternal.h"
32 #include "insets/InsetFloat.h"
33 #include "insets/InsetFloatList.h"
34 #include "insets/InsetFoot.h"
35 #include "insets/InsetGraphics.h"
36 #include "insets/InsetInclude.h"
37 #include "insets/InsetIndex.h"
38 #include "insets/InsetInfo.h"
39 #include "insets/InsetNomencl.h"
40 #include "insets/InsetLabel.h"
41 #include "insets/InsetLine.h"
42 #include "insets/InsetMarginal.h"
43 #include "insets/InsetNewline.h"
44 #include "insets/InsetNewpage.h"
45 #include "insets/InsetNote.h"
46 #include "insets/InsetBox.h"
47 #include "insets/InsetBranch.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 <boost/assert.hpp>
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, buf.params().documentClassPtr(), 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_ERT_INSERT:
131 return new InsetERT(buf);
133 case LFUN_LISTING_INSERT:
134 return new InsetListings(buf);
136 case LFUN_FOOTNOTE_INSERT:
137 return new InsetFoot(buf);
139 case LFUN_MARGINALNOTE_INSERT:
140 return new InsetMarginal(buf);
142 case LFUN_OPTIONAL_INSERT:
143 return new InsetOptArg(buf);
145 case LFUN_BIBITEM_INSERT:
146 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
148 case LFUN_FLOAT_INSERT: {
149 // check if the float type exists
150 string const argument = to_utf8(cmd.argument());
151 if (buf.params().documentClass().floats().typeExist(argument))
152 return new InsetFloat(buf, argument);
153 lyxerr << "Non-existent float type: " << argument << endl;
156 case LFUN_FLOAT_WIDE_INSERT: {
157 // check if the float type exists
158 string const argument = to_utf8(cmd.argument());
159 if (params.documentClass().floats().typeExist(argument)) {
160 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
161 p->setWide(true, params);
164 lyxerr << "Non-existent float type: " << argument << endl;
168 case LFUN_WRAP_INSERT: {
169 string const argument = to_utf8(cmd.argument());
170 if (argument == "figure" || argument == "table")
171 return new InsetWrap(buf, argument);
172 lyxerr << "Non-existent wrapfig type: " << argument << endl;
176 case LFUN_INDEX_INSERT:
177 return new InsetIndex(buf);
179 case LFUN_NOMENCL_INSERT: {
180 InsetCommandParams icp(NOMENCL_CODE);
181 icp["symbol"] = cmd.argument();
182 return new InsetNomencl(icp);
185 case LFUN_TABULAR_INSERT: {
186 if (cmd.argument().empty())
188 istringstream ss(to_utf8(cmd.argument()));
195 return new InsetTabular(buf, r, c);
198 case LFUN_CAPTION_INSERT:
199 return new InsetCaption(buf);
201 case LFUN_INDEX_PRINT:
202 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
204 case LFUN_NOMENCL_PRINT:
205 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
207 case LFUN_TOC_INSERT:
208 return new InsetTOC(InsetCommandParams(TOC_CODE));
210 case LFUN_ENVIRONMENT_INSERT:
211 return new InsetEnvironment(buf, cmd.argument());
213 case LFUN_INFO_INSERT:
214 return new InsetInfo(buf, to_utf8(cmd.argument()));
216 case LFUN_INSET_INSERT: {
217 string const name = cmd.getArg(0);
218 InsetCode code = insetCode(name);
221 lyxerr << "No such inset '" << name << "'.";
225 InsetCommandParams icp(code);
226 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
227 return new InsetBibitem(icp);
231 InsetCommandParams icp(code);
232 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
233 return new InsetBibtex(icp);
237 InsetCommandParams icp(code);
238 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
239 return new InsetCitation(icp);
243 return new InsetERT(buf,
244 InsetERT::string2params(to_utf8(cmd.argument())));
247 case LISTINGS_CODE: {
248 InsetListingsParams par;
249 InsetListings::string2params(to_utf8(cmd.argument()), par);
250 return new InsetListings(buf, par);
253 case EXTERNAL_CODE: {
254 InsetExternalParams iep;
255 InsetExternal::string2params(to_utf8(cmd.argument()), buf, iep);
256 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
257 inset->setBuffer(buf);
258 inset->setParams(iep);
259 return inset.release();
262 case GRAPHICS_CODE: {
263 InsetGraphicsParams igp;
264 InsetGraphics::string2params(to_utf8(cmd.argument()), buf, igp);
265 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
266 inset->setParams(igp);
267 return inset.release();
270 case HYPERLINK_CODE: {
271 InsetCommandParams icp(code);
272 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
273 return new InsetHyperlink(icp);
277 InsetCommandParams icp(code);
278 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
279 return new InsetInclude(icp);
283 return new InsetIndex(buf);
286 InsetCommandParams icp(code);
287 InsetCommand::string2params(name, lyx::to_utf8(cmd.argument()), icp);
288 return new InsetNomencl(icp);
292 InsetCommandParams icp(code);
293 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
294 return new InsetLabel(icp);
298 InsetCommandParams icp(code);
299 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
300 return new InsetRef(buf, icp);
304 InsetSpaceParams isp;
305 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
306 return new InsetSpace(isp);
310 InsetCommandParams icp(code);
311 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
312 return new InsetTOC(icp);
317 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
318 return new InsetVSpace(vspace);
322 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
327 } //end LFUN_INSET_INSERT
329 case LFUN_SPACE_INSERT: {
330 string const name = cmd.getArg(0);
331 string const len = cmd.getArg(1);
333 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
336 InsetSpaceParams isp;
337 if (name == "normal")
338 isp.kind = InsetSpaceParams::NORMAL;
339 else if (name == "protected")
340 isp.kind = InsetSpaceParams::PROTECTED;
341 else if (name == "thin")
342 isp.kind = InsetSpaceParams::THIN;
343 else if (name == "quad")
344 isp.kind = InsetSpaceParams::QUAD;
345 else if (name == "qquad")
346 isp.kind = InsetSpaceParams::QQUAD;
347 else if (name == "enspace")
348 isp.kind = InsetSpaceParams::ENSPACE;
349 else if (name == "enskip")
350 isp.kind = InsetSpaceParams::ENSKIP;
351 else if (name == "negthinspace")
352 isp.kind = InsetSpaceParams::NEGTHIN;
353 else if (name == "hfill")
354 isp.kind = InsetSpaceParams::HFILL;
355 else if (name == "hfill*")
356 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
357 else if (name == "dotfill")
358 isp.kind = InsetSpaceParams::DOTFILL;
359 else if (name == "hrulefill")
360 isp.kind = InsetSpaceParams::HRULEFILL;
361 else if (name == "hspace") {
362 if (len.empty() || !isValidLength(len)) {
363 lyxerr << "LyX function 'space-insert hspace' "
364 << "needs a valid length argument." << endl;
367 isp.kind = InsetSpaceParams::CUSTOM;
368 isp.length = Length(len);
370 else if (name == "hspace*") {
371 if (len.empty() || !isValidLength(len)) {
372 lyxerr << "LyX function 'space-insert hspace*' "
373 << "needs a valid length argument." << endl;
376 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
377 isp.length = Length(len);
380 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
383 return new InsetSpace(isp);
391 } catch (ExceptionMessage const & message) {
392 if (message.type_ == ErrorException) {
393 // This should never happen!
394 Alert::error(message.title_, message.details_);
396 } else if (message.type_ == WarningException) {
397 Alert::warning(message.title_, message.details_);
406 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
408 Inset * inset = createInsetHelper(buf, cmd);
410 inset->setBuffer(buf);
415 Inset * readInset(Lexer & lex, Buffer const & buf)
418 if (lex.getString() != "\\begin_inset")
419 LYXERR0("Buffer::readInset: Consistency check failed.");
421 auto_ptr<Inset> inset;
424 string tmptok = lex.getString();
426 // test the different insets
428 // FIXME It would be better if we did not have this branch and could
429 // just do one massive switch for all insets. But at present, it's
430 // easier to do it this way, and we can't do the massive switch until
431 // the conversion mentioned below. Note that if we do want to do a
432 // single switch, we need to remove this "CommandInset" line---or
433 // replace it with a single "InsetType" line that would be used in all
435 if (tmptok == "CommandInset") {
437 string const insetType = lex.getString();
438 lex.pushToken(insetType);
440 InsetCode const code = insetCode(insetType);
442 //FIXME If we do the one massive switch, we cannot do this here, since
443 //we do not know in advance that we're dealing with a command inset.
444 //Worst case, we could put it in each case below. Better, we could
445 //pass the lexer to the constructor and let the params be built there.
446 InsetCommandParams inscmd(code);
451 inset.reset(new InsetBibitem(inscmd));
454 inset.reset(new InsetBibtex(inscmd));
457 inset.reset(new InsetCitation(inscmd));
460 inset.reset(new InsetHyperlink(inscmd));
463 inset.reset(new InsetInclude(inscmd));
465 case INDEX_PRINT_CODE:
466 inset.reset(new InsetPrintIndex(inscmd));
469 inset.reset(new InsetLabel(inscmd));
472 inset.reset(new InsetNomencl(inscmd));
474 case NOMENCL_PRINT_CODE:
475 inset.reset(new InsetPrintNomencl(inscmd));
478 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,
518 buf.params().documentClassPtr(), s));
519 } else if (tmptok == "Branch") {
520 inset.reset(new InsetBranch(buf,
521 InsetBranchParams()));
522 } else if (tmptok == "Environment") {
524 inset.reset(new InsetEnvironment(buf, lex.getDocString()));
525 } else if (tmptok == "ERT") {
526 inset.reset(new InsetERT(buf));
527 } else if (tmptok == "listings") {
528 inset.reset(new InsetListings(buf));
529 } else if (tmptok == "Space") {
530 inset.reset(new InsetSpace);
531 } else if (tmptok == "Tabular") {
532 inset.reset(new InsetTabular(buf));
533 } else if (tmptok == "Text") {
534 inset.reset(new InsetText(buf));
535 } else if (tmptok == "VSpace") {
536 inset.reset(new InsetVSpace);
537 } else if (tmptok == "Foot") {
538 inset.reset(new InsetFoot(buf));
539 } else if (tmptok == "Marginal") {
540 inset.reset(new InsetMarginal(buf));
541 } else if (tmptok == "Newpage") {
542 inset.reset(new InsetNewpage);
543 } else if (tmptok == "Newline") {
544 inset.reset(new InsetNewline);
545 } else if (tmptok == "OptArg") {
546 inset.reset(new InsetOptArg(buf));
547 } else if (tmptok == "Float") {
549 string tmptok = lex.getString();
550 inset.reset(new InsetFloat(buf, tmptok));
551 } else if (tmptok == "Wrap") {
553 string tmptok = lex.getString();
554 inset.reset(new InsetWrap(buf, tmptok));
556 } else if (tmptok == "Theorem") {
557 inset.reset(new InsetList);
559 } else if (tmptok == "Caption") {
560 inset.reset(new InsetCaption(buf));
561 } else if (tmptok == "Index") {
562 inset.reset(new InsetIndex(buf));
563 } else if (tmptok == "FloatList") {
564 inset.reset(new InsetFloatList);
565 } else if (tmptok == "Info") {
566 inset.reset(new InsetInfo(buf));
568 lyxerr << "unknown Inset type '" << tmptok
570 while (lex.isOK() && lex.getString() != "\\end_inset")
575 // Set the buffer reference for proper parsing of some insets
576 // (InsetCollapsable for example)
577 inset->setBuffer(const_cast<Buffer &>(buf));
579 // Set again the buffer for insets that are created inside this inset
580 // (InsetMathHull for example).
581 inset->setBuffer(const_cast<Buffer &>(buf));
583 return inset.release();