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/InsetNote.h"
44 #include "insets/InsetBox.h"
45 #include "insets/InsetBranch.h"
46 #include "insets/InsetOptArg.h"
47 #include "insets/InsetNewpage.h"
48 #include "insets/InsetRef.h"
49 #include "insets/InsetSpace.h"
50 #include "insets/InsetTabular.h"
51 #include "insets/InsetTOC.h"
52 #include "insets/InsetHyperlink.h"
53 #include "insets/InsetVSpace.h"
54 #include "insets/InsetWrap.h"
56 #include "mathed/MathMacroTemplate.h"
57 #include "mathed/InsetMathHull.h"
59 #include "frontends/alert.h"
61 #include "support/debug.h"
62 #include "support/lstrings.h"
63 #include "support/ExceptionMessage.h"
65 #include <boost/assert.hpp>
70 using namespace lyx::support;
74 namespace Alert = frontend::Alert;
77 Inset * createInsetHelper(Buffer & buf, FuncRequest const & cmd)
79 BufferParams const & params = buf.params();
85 case LFUN_LINE_INSERT:
88 case LFUN_NEWPAGE_INSERT:
89 return new InsetNewpage;
91 case LFUN_PAGEBREAK_INSERT:
92 return new InsetPagebreak;
94 case LFUN_CLEARPAGE_INSERT:
95 return new InsetClearPage;
97 case LFUN_CLEARDOUBLEPAGE_INSERT:
98 return new InsetClearDoublePage;
100 case LFUN_FLEX_INSERT: {
101 string s = cmd.getArg(0);
102 return new InsetFlex(buf, buf.params().documentClassPtr(), s);
105 case LFUN_NOTE_INSERT: {
106 string arg = cmd.getArg(0);
109 return new InsetNote(buf, arg);
112 case LFUN_BOX_INSERT: {
113 string arg = cmd.getArg(0);
116 return new InsetBox(buf, arg);
119 case LFUN_BRANCH_INSERT: {
120 docstring arg = cmd.argument();
122 arg = from_ascii("none");
123 return new InsetBranch(buf, InsetBranchParams(arg));
126 case LFUN_ERT_INSERT:
127 return new InsetERT(buf);
129 case LFUN_LISTING_INSERT:
130 return new InsetListings(buf);
132 case LFUN_FOOTNOTE_INSERT:
133 return new InsetFoot(buf);
135 case LFUN_MARGINALNOTE_INSERT:
136 return new InsetMarginal(buf);
138 case LFUN_OPTIONAL_INSERT:
139 return new InsetOptArg(buf);
141 case LFUN_BIBITEM_INSERT:
142 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
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->wide(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_ENVIRONMENT_INSERT:
207 return new InsetEnvironment(buf, cmd.argument());
209 case LFUN_INFO_INSERT:
210 return 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 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
223 return new InsetBibitem(icp);
227 InsetCommandParams icp(code);
228 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
229 return new InsetBibtex(icp);
233 InsetCommandParams icp(code);
234 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
235 return new InsetCitation(icp);
239 InsetCollapsable::CollapseStatus st;
240 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
241 return new InsetERT(buf, st);
244 case LISTINGS_CODE: {
245 InsetListingsParams par;
246 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
247 return new InsetListings(buf, par);
250 case EXTERNAL_CODE: {
251 InsetExternalParams iep;
252 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
253 auto_ptr<InsetExternal> inset(new InsetExternal);
254 inset->setBuffer(buf);
255 inset->setParams(iep);
256 return inset.release();
259 case GRAPHICS_CODE: {
260 InsetGraphicsParams igp;
261 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
262 auto_ptr<InsetGraphics> inset(new InsetGraphics);
263 inset->setParams(igp);
264 return inset.release();
267 case HYPERLINK_CODE: {
268 InsetCommandParams icp(code);
269 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
270 return new InsetHyperlink(icp);
274 InsetCommandParams icp(code);
275 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
276 return new InsetInclude(icp);
280 return new InsetIndex(buf);
283 InsetCommandParams icp(code);
284 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
285 return new InsetNomencl(icp);
289 InsetCommandParams icp(code);
290 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
291 return new InsetLabel(icp);
295 InsetCommandParams icp(code);
296 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
297 return new InsetRef(buf, icp);
301 InsetSpaceParams isp;
302 InsetSpaceMailer::string2params(to_utf8(cmd.argument()), isp);
303 return new InsetSpace(isp);
307 InsetCommandParams icp(code);
308 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
309 return new InsetTOC(icp);
314 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
315 return new InsetVSpace(vspace);
319 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
324 } //end LFUN_INSET_INSERT
326 case LFUN_SPACE_INSERT: {
327 string const name = cmd.getArg(0);
328 string const len = cmd.getArg(1);
330 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
333 InsetSpaceParams isp;
334 if (name == "normal")
335 isp.kind = InsetSpaceParams::NORMAL;
336 else if (name == "protected")
337 isp.kind = InsetSpaceParams::PROTECTED;
338 else if (name == "thin")
339 isp.kind = InsetSpaceParams::THIN;
340 else if (name == "quad")
341 isp.kind = InsetSpaceParams::QUAD;
342 else if (name == "qquad")
343 isp.kind = InsetSpaceParams::QQUAD;
344 else if (name == "enspace")
345 isp.kind = InsetSpaceParams::ENSPACE;
346 else if (name == "enskip")
347 isp.kind = InsetSpaceParams::ENSKIP;
348 else if (name == "negthinspace")
349 isp.kind = InsetSpaceParams::NEGTHIN;
350 else if (name == "hfill")
351 isp.kind = InsetSpaceParams::HFILL;
352 else if (name == "hfill*")
353 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
354 else if (name == "dotfill")
355 isp.kind = InsetSpaceParams::DOTFILL;
356 else if (name == "hrulefill")
357 isp.kind = InsetSpaceParams::HRULEFILL;
358 else if (name == "hspace") {
359 if (len.empty() || !isValidLength(len)) {
360 lyxerr << "LyX function 'space-insert hspace' "
361 << "needs a valid length argument." << endl;
364 isp.kind = InsetSpaceParams::CUSTOM;
365 isp.length = Length(len);
367 else if (name == "hspace*") {
368 if (len.empty() || !isValidLength(len)) {
369 lyxerr << "LyX function 'space-insert hspace*' "
370 << "needs a valid length argument." << endl;
373 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
374 isp.length = Length(len);
377 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
380 return new InsetSpace(isp);
388 } catch (ExceptionMessage const & message) {
389 if (message.type_ == ErrorException) {
390 // This should never happen!
391 Alert::error(message.title_, message.details_);
393 } else if (message.type_ == WarningException) {
394 Alert::warning(message.title_, message.details_);
403 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
405 Inset * inset = createInsetHelper(buf, cmd);
407 inset->setBuffer(buf);
411 Inset * readInset(Lexer & lex, Buffer const & buf)
414 if (lex.getString() != "\\begin_inset") {
415 lyxerr << "Buffer::readInset: Consistency check failed."
419 auto_ptr<Inset> inset;
422 string tmptok = lex.getString();
424 // test the different insets
426 //FIXME It would be better if we did not have this branch and could
427 //just do one massive switch for all insets. But at present, it's easier
428 //to do it this way, and we can't do the massive switch until the conversion
430 //Note that if we do want to do a single switch, we need to remove
431 //this "CommandInset" line---or replace it with a single "InsetType" line
432 //that would be used in all insets.
433 if (tmptok == "CommandInset") {
435 string const insetType = lex.getString();
436 lex.pushToken(insetType);
438 InsetCode const code = insetCode(insetType);
440 //FIXME If we do the one massive switch, we cannot do this here, since
441 //we do not know in advance that we're dealing with a command inset.
442 //Worst case, we could put it in each case below. Better, we could
443 //pass the lexer to the constructor and let the params be built there.
444 InsetCommandParams inscmd(code);
449 inset.reset(new InsetBibitem(inscmd));
452 inset.reset(new InsetBibtex(inscmd));
455 inset.reset(new InsetCitation(inscmd));
458 inset.reset(new InsetHyperlink(inscmd));
461 inset.reset(new InsetInclude(inscmd));
463 case INDEX_PRINT_CODE:
464 inset.reset(new InsetPrintIndex(inscmd));
467 inset.reset(new InsetLabel(inscmd));
470 inset.reset(new InsetNomencl(inscmd));
472 case NOMENCL_PRINT_CODE:
473 inset.reset(new InsetPrintNomencl(inscmd));
476 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
477 inset.reset(new InsetRef(buf, inscmd));
480 inset.reset(new InsetTOC(inscmd));
484 lyxerr << "unknown CommandInset '" << insetType
486 while (lex.isOK() && lex.getString() != "\\end_inset")
490 inset->setBuffer(const_cast<Buffer &>(buf));
492 // FIXME This branch should be made to use inset codes as the preceding
493 // branch does. Unfortunately, that will take some doing. It requires
494 // converting the representation of the insets in LyX files so that they
495 // use the inset names listed in Inset.cpp. Then, as above, the inset names
496 // can be translated to inset codes using insetCode(). And the insets'
497 // write() routines should use insetName() rather than hardcoding it.
498 if (tmptok == "Quotes") {
499 inset.reset(new InsetQuotes);
500 } else if (tmptok == "External") {
501 inset.reset(new InsetExternal);
502 } else if (tmptok == "FormulaMacro") {
503 inset.reset(new MathMacroTemplate);
504 } else if (tmptok == "Formula") {
505 inset.reset(new InsetMathHull);
506 } else if (tmptok == "Graphics") {
507 inset.reset(new InsetGraphics);
508 } else if (tmptok == "Note") {
509 inset.reset(new InsetNote(buf, tmptok));
510 } else if (tmptok == "Box") {
511 inset.reset(new InsetBox(buf, tmptok));
512 } else if (tmptok == "Flex") {
514 string s = lex.getString();
515 inset.reset(new InsetFlex(buf,
516 buf.params().documentClassPtr(), s));
517 } else if (tmptok == "Branch") {
518 inset.reset(new InsetBranch(buf,
519 InsetBranchParams()));
520 } else if (tmptok == "Environment") {
522 inset.reset(new InsetEnvironment(buf, lex.getDocString()));
523 } else if (tmptok == "ERT") {
524 inset.reset(new InsetERT(buf));
525 } else if (tmptok == "listings") {
526 inset.reset(new InsetListings(buf));
527 } else if (tmptok == "Space") {
528 inset.reset(new InsetSpace);
529 } else if (tmptok == "Tabular") {
530 inset.reset(new InsetTabular(buf));
531 } else if (tmptok == "Text") {
532 inset.reset(new InsetText(buf));
533 } else if (tmptok == "VSpace") {
534 inset.reset(new InsetVSpace);
535 } else if (tmptok == "Foot") {
536 inset.reset(new InsetFoot(buf));
537 } else if (tmptok == "Marginal") {
538 inset.reset(new InsetMarginal(buf));
539 } else if (tmptok == "OptArg") {
540 inset.reset(new InsetOptArg(buf));
541 } else if (tmptok == "Float") {
543 string tmptok = lex.getString();
544 inset.reset(new InsetFloat(buf, tmptok));
545 } else if (tmptok == "Wrap") {
547 string tmptok = lex.getString();
548 inset.reset(new InsetWrap(buf, tmptok));
550 } else if (tmptok == "Theorem") {
551 inset.reset(new InsetList);
553 } else if (tmptok == "Caption") {
554 inset.reset(new InsetCaption(buf));
555 } else if (tmptok == "Index") {
556 inset.reset(new InsetIndex(buf));
557 } else if (tmptok == "FloatList") {
558 inset.reset(new InsetFloatList);
559 } else if (tmptok == "Info") {
560 inset.reset(new InsetInfo(buf));
562 lyxerr << "unknown Inset type '" << tmptok
564 while (lex.isOK() && lex.getString() != "\\end_inset")
569 // Set the buffer reference for proper parsing of some insets
570 // (InsetCollapsable for example)
571 inset->setBuffer(const_cast<Buffer &>(buf));
573 // Set again the buffer for insets that are created inside this inset
574 // (InsetMathHull for example).
575 inset->setBuffer(const_cast<Buffer &>(buf));
577 return inset.release();