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/InsetHFill.h"
37 #include "insets/InsetInclude.h"
38 #include "insets/InsetIndex.h"
39 #include "insets/InsetInfo.h"
40 #include "insets/InsetNomencl.h"
41 #include "insets/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.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 <boost/assert.hpp>
74 namespace Alert = frontend::Alert;
77 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
79 BufferParams const & params = buf.params();
84 case LFUN_HFILL_INSERT:
85 return new InsetHFill;
87 case LFUN_LINE_INSERT:
90 case LFUN_NEWPAGE_INSERT:
91 return new InsetNewpage;
93 case LFUN_PAGEBREAK_INSERT:
94 return new InsetPagebreak;
96 case LFUN_CLEARPAGE_INSERT:
97 return new InsetClearPage;
99 case LFUN_CLEARDOUBLEPAGE_INSERT:
100 return new InsetClearDoublePage;
102 case LFUN_FLEX_INSERT: {
103 string s = cmd.getArg(0);
104 TextClass const & tclass = params.getTextClass();
105 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
106 return new InsetFlex(params, il);
109 case LFUN_NOTE_INSERT: {
110 string arg = cmd.getArg(0);
113 return new InsetNote(params, arg);
116 case LFUN_BOX_INSERT: {
117 string arg = cmd.getArg(0);
120 return new InsetBox(params, arg);
123 case LFUN_BRANCH_INSERT: {
124 docstring arg = cmd.argument();
126 arg = from_ascii("none");
127 return new InsetBranch(params, InsetBranchParams(arg));
130 case LFUN_ERT_INSERT:
131 return new InsetERT(params);
133 case LFUN_LISTING_INSERT:
134 return new InsetListings(params);
136 case LFUN_FOOTNOTE_INSERT:
137 return new InsetFoot(params);
139 case LFUN_MARGINALNOTE_INSERT:
140 return new InsetMarginal(params);
142 case LFUN_OPTIONAL_INSERT:
143 return new InsetOptArg(params);
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 (params.getTextClass().floats().typeExist(argument))
152 return new InsetFloat(params, 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.getTextClass().floats().typeExist(argument)) {
160 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
161 p->wide(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(params, argument);
172 lyxerr << "Non-existent wrapfig type: " << argument << endl;
176 case LFUN_INDEX_INSERT:
177 return new InsetIndex(params);
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 auto_ptr<InsetCaption> inset(new InsetCaption(params));
200 inset->setAutoBreakRows(true);
201 inset->setDrawFrame(true);
202 inset->setFrameColor(Color_captionframe);
203 return inset.release();
206 case LFUN_INDEX_PRINT:
207 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
209 case LFUN_NOMENCL_PRINT:
210 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
212 case LFUN_TOC_INSERT:
213 return new InsetTOC(InsetCommandParams(TOC_CODE));
215 case LFUN_ENVIRONMENT_INSERT:
216 return new InsetEnvironment(params, cmd.argument());
218 case LFUN_INFO_INSERT:
219 return new InsetInfo(params, to_utf8(cmd.argument()));
221 case LFUN_THEOREM_INSERT:
222 return new InsetTheorem;
225 case LFUN_INSET_INSERT: {
226 string const name = cmd.getArg(0);
227 InsetCode code = insetCode(name);
230 lyxerr << "No such inset '" << name << "'.";
234 InsetCommandParams icp(code);
235 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
236 return new InsetBibitem(icp);
240 InsetCommandParams icp(code);
241 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
242 return new InsetBibtex(icp);
246 InsetCommandParams icp(code);
247 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
248 return new InsetCitation(icp);
252 InsetCollapsable::CollapseStatus st;
253 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
254 return new InsetERT(params, st);
257 case LISTINGS_CODE: {
258 InsetListingsParams par;
259 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
260 return new InsetListings(params, par);
263 case EXTERNAL_CODE: {
264 InsetExternalParams iep;
265 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
266 auto_ptr<InsetExternal> inset(new InsetExternal);
267 inset->setParams(iep, buf);
268 return inset.release();
271 case GRAPHICS_CODE: {
272 InsetGraphicsParams igp;
273 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
274 auto_ptr<InsetGraphics> inset(new InsetGraphics);
275 inset->setParams(igp);
276 return inset.release();
279 case HYPERLINK_CODE: {
280 InsetCommandParams icp(code);
281 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
282 return new InsetHyperlink(icp);
286 InsetCommandParams icp(code);
287 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
288 return new InsetInclude(icp);
292 return new InsetIndex(params);
295 InsetCommandParams icp(code);
296 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
297 return new InsetNomencl(icp);
301 InsetCommandParams icp(code);
302 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
303 return new InsetLabel(icp);
307 InsetCommandParams icp(code);
308 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
309 return new InsetRef(icp, buf);
313 InsetCommandParams icp(code);
314 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
315 return new InsetTOC(icp);
320 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
321 return new InsetVSpace(vspace);
325 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
330 } //end LFUN_INSET_INSERT
332 case LFUN_SPACE_INSERT: {
333 string const name = to_utf8(cmd.argument());
334 if (name == "normal")
335 return new InsetSpace(InsetSpace::NORMAL);
336 if (name == "protected")
337 return new InsetSpace(InsetSpace::PROTECTED);
339 return new InsetSpace(InsetSpace::THIN);
341 return new InsetSpace(InsetSpace::QUAD);
343 return new InsetSpace(InsetSpace::QQUAD);
344 if (name == "enspace")
345 return new InsetSpace(InsetSpace::ENSPACE);
346 if (name == "enskip")
347 return new InsetSpace(InsetSpace::ENSKIP);
348 if (name == "negthinspace")
349 return new InsetSpace(InsetSpace::NEGTHIN);
351 lyxerr << "LyX function 'space' needs an argument." << endl;
353 lyxerr << "Wrong argument for LyX function 'space'." << endl;
361 } catch (support::ExceptionMessage const & message) {
362 if (message.type_ == support::ErrorException) {
363 Alert::error(message.title_, message.details_);
364 LyX::cref().emergencyCleanup();
366 } else if (message.type_ == support::WarningException) {
367 Alert::warning(message.title_, message.details_);
377 Inset * readInset(Lexer & lex, Buffer const & buf)
380 if (lex.getString() != "\\begin_inset") {
381 lyxerr << "Buffer::readInset: Consistency check failed."
385 auto_ptr<Inset> inset;
387 TextClass const & tclass = buf.params().getTextClass();
390 string tmptok = lex.getString();
392 // test the different insets
394 //FIXME It would be better if we did not have this branch and could
395 //just do one massive switch for all insets. But at present, it's easier
396 //to do it this way, and we can't do the massive switch until the conversion
398 //Note that if we do want to do a single switch, we need to remove
399 //this "CommandInset" line---or replace it with a single "InsetType" line
400 //that would be used in all insets.
401 if (tmptok == "CommandInset") {
403 string const insetType = lex.getString();
404 lex.pushToken(insetType);
406 InsetCode const code = insetCode(insetType);
408 //FIXME If we do the one massive switch, we cannot do this here, since
409 //we do not know in advance that we're dealing with a command inset.
410 //Worst case, we could put it in each case below. Better, we could
411 //pass the lexer to the constructor and let the params be built there.
412 InsetCommandParams inscmd(code);
417 inset.reset(new InsetBibitem(inscmd));
420 inset.reset(new InsetBibtex(inscmd));
423 inset.reset(new InsetCitation(inscmd));
426 inset.reset(new InsetHyperlink(inscmd));
429 inset.reset(new InsetInclude(inscmd));
431 case INDEX_PRINT_CODE:
432 inset.reset(new InsetPrintIndex(inscmd));
435 inset.reset(new InsetLabel(inscmd));
438 inset.reset(new InsetNomencl(inscmd));
440 case NOMENCL_PRINT_CODE:
441 inset.reset(new InsetPrintNomencl(inscmd));
444 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
445 inset.reset(new InsetRef(inscmd, buf));
448 inset.reset(new InsetTOC(inscmd));
452 lyxerr << "unknown CommandInset '" << insetType
454 while (lex.isOK() && lex.getString() != "\\end_inset")
459 // FIXME This branch should be made to use inset codes as the preceding
460 // branch does. Unfortunately, that will take some doing. It requires
461 // converting the representation of the insets in LyX files so that they
462 // use the inset names listed in Inset.cpp. Then, as above, the inset names
463 // can be translated to inset codes using insetCode(). And the insets'
464 // write() routines should use insetName() rather than hardcoding it.
465 if (tmptok == "Quotes") {
466 inset.reset(new InsetQuotes);
467 } else if (tmptok == "External") {
468 inset.reset(new InsetExternal);
469 } else if (tmptok == "FormulaMacro") {
470 inset.reset(new MathMacroTemplate);
471 } else if (tmptok == "Formula") {
472 inset.reset(new InsetMathHull);
473 } else if (tmptok == "Graphics") {
474 inset.reset(new InsetGraphics);
475 } else if (tmptok == "Note") {
476 inset.reset(new InsetNote(buf.params(), tmptok));
477 } else if (tmptok == "Box") {
478 inset.reset(new InsetBox(buf.params(), tmptok));
479 } else if (tmptok == "Flex") {
481 string s = lex.getString();
482 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
483 inset.reset(new InsetFlex(buf.params(), il));
484 } else if (tmptok == "Branch") {
485 inset.reset(new InsetBranch(buf.params(),
486 InsetBranchParams()));
487 } else if (tmptok == "Environment") {
489 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
490 } else if (tmptok == "ERT") {
491 inset.reset(new InsetERT(buf.params()));
492 } else if (tmptok == "listings") {
493 inset.reset(new InsetListings(buf.params()));
494 } else if (tmptok == "InsetSpace") {
495 inset.reset(new InsetSpace);
496 } else if (tmptok == "Tabular") {
497 inset.reset(new InsetTabular(buf));
498 } else if (tmptok == "Text") {
499 inset.reset(new InsetText(buf.params()));
500 } else if (tmptok == "VSpace") {
501 inset.reset(new InsetVSpace);
502 } else if (tmptok == "Foot") {
503 inset.reset(new InsetFoot(buf.params()));
504 } else if (tmptok == "Marginal") {
505 inset.reset(new InsetMarginal(buf.params()));
506 } else if (tmptok == "OptArg") {
507 inset.reset(new InsetOptArg(buf.params()));
508 } else if (tmptok == "Float") {
510 string tmptok = lex.getString();
511 inset.reset(new InsetFloat(buf.params(), tmptok));
512 } else if (tmptok == "Wrap") {
514 string tmptok = lex.getString();
515 inset.reset(new InsetWrap(buf.params(), tmptok));
517 } else if (tmptok == "Theorem") {
518 inset.reset(new InsetList);
520 } else if (tmptok == "Caption") {
521 inset.reset(new InsetCaption(buf.params()));
522 } else if (tmptok == "Index") {
523 inset.reset(new InsetIndex(buf.params()));
524 } else if (tmptok == "FloatList") {
525 inset.reset(new InsetFloatList);
526 } else if (tmptok == "Info") {
527 inset.reset(new InsetInfo(buf.params()));
529 lyxerr << "unknown Inset type '" << tmptok
531 while (lex.isOK() && lex.getString() != "\\end_inset")
536 inset->read(buf, lex);
539 return inset.release();