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>
77 namespace Alert = frontend::Alert;
80 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
82 BufferParams const & params = buf.params();
87 case LFUN_HFILL_INSERT:
88 return new InsetHFill;
90 case LFUN_LINE_INSERT:
93 case LFUN_NEWPAGE_INSERT:
94 return new InsetNewpage;
96 case LFUN_PAGEBREAK_INSERT:
97 return new InsetPagebreak;
99 case LFUN_CLEARPAGE_INSERT:
100 return new InsetClearPage;
102 case LFUN_CLEARDOUBLEPAGE_INSERT:
103 return new InsetClearDoublePage;
105 case LFUN_FLEX_INSERT: {
106 string s = cmd.getArg(0);
107 TextClass const & tclass = params.getTextClass();
108 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
109 return new InsetFlex(params, il);
112 case LFUN_NOTE_INSERT: {
113 string arg = cmd.getArg(0);
116 return new InsetNote(params, arg);
119 case LFUN_BOX_INSERT: {
120 string arg = cmd.getArg(0);
123 return new InsetBox(params, arg);
126 case LFUN_BRANCH_INSERT: {
127 docstring arg = cmd.argument();
129 arg = from_ascii("none");
130 return new InsetBranch(params, InsetBranchParams(arg));
133 case LFUN_ERT_INSERT:
134 return new InsetERT(params);
136 case LFUN_LISTING_INSERT:
137 return new InsetListings(params);
139 case LFUN_FOOTNOTE_INSERT:
140 return new InsetFoot(params);
142 case LFUN_MARGINALNOTE_INSERT:
143 return new InsetMarginal(params);
145 case LFUN_OPTIONAL_INSERT:
146 return new InsetOptArg(params);
148 case LFUN_BIBITEM_INSERT:
149 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
151 case LFUN_FLOAT_INSERT: {
152 // check if the float type exists
153 string const argument = to_utf8(cmd.argument());
154 if (params.getTextClass().floats().typeExist(argument))
155 return new InsetFloat(params, 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.getTextClass().floats().typeExist(argument)) {
163 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
164 p->wide(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(params, argument);
175 lyxerr << "Non-existent wrapfig type: " << argument << endl;
179 case LFUN_INDEX_INSERT:
180 return new InsetIndex(params);
182 case LFUN_NOMENCL_INSERT: {
183 InsetCommandParams icp(NOMENCL_CODE);
184 icp["symbol"] = cmd.argument();
185 return new InsetNomencl(icp);
188 case LFUN_TABULAR_INSERT: {
189 if (cmd.argument().empty())
191 std::istringstream ss(to_utf8(cmd.argument()));
198 return new InsetTabular(buf, r, c);
201 case LFUN_CAPTION_INSERT: {
202 auto_ptr<InsetCaption> inset(new InsetCaption(params));
203 inset->setAutoBreakRows(true);
204 inset->setDrawFrame(true);
205 inset->setFrameColor(Color_captionframe);
206 return inset.release();
209 case LFUN_INDEX_PRINT:
210 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
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_ENVIRONMENT_INSERT:
219 return new InsetEnvironment(params, cmd.argument());
221 case LFUN_INFO_INSERT:
222 return new InsetInfo(params, to_utf8(cmd.argument()));
224 case LFUN_THEOREM_INSERT:
225 return new InsetTheorem;
228 case LFUN_INSET_INSERT: {
229 string const name = cmd.getArg(0);
230 InsetCode code = insetCode(name);
233 lyxerr << "No such inset '" << name << "'.";
237 InsetCommandParams icp(code);
238 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
239 return new InsetBibitem(icp);
243 InsetCommandParams icp(code);
244 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
245 return new InsetBibtex(icp);
249 InsetCommandParams icp(code);
250 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
251 return new InsetCitation(icp);
255 InsetCollapsable::CollapseStatus st;
256 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
257 return new InsetERT(params, st);
260 case LISTINGS_CODE: {
261 InsetListingsParams par;
262 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
263 return new InsetListings(params, par);
266 case EXTERNAL_CODE: {
267 InsetExternalParams iep;
268 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
269 auto_ptr<InsetExternal> inset(new InsetExternal);
270 inset->setParams(iep, buf);
271 return inset.release();
274 case GRAPHICS_CODE: {
275 InsetGraphicsParams igp;
276 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
277 auto_ptr<InsetGraphics> inset(new InsetGraphics);
278 inset->setParams(igp);
279 return inset.release();
282 case HYPERLINK_CODE: {
283 InsetCommandParams icp(code);
284 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
285 return new InsetHyperlink(icp);
289 InsetCommandParams icp(code);
290 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
291 return new InsetInclude(icp);
295 return new InsetIndex(params);
298 InsetCommandParams icp(code);
299 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
300 return new InsetNomencl(icp);
304 InsetCommandParams icp(code);
305 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
306 return new InsetLabel(icp);
310 InsetCommandParams icp(code);
311 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
312 return new InsetRef(icp, buf);
316 InsetCommandParams icp(code);
317 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
318 return new InsetTOC(icp);
323 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
324 return new InsetVSpace(vspace);
328 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
333 } //end LFUN_INSET_INSERT
335 case LFUN_SPACE_INSERT: {
336 string const name = to_utf8(cmd.argument());
337 if (name == "normal")
338 return new InsetSpace(InsetSpace::NORMAL);
339 if (name == "protected")
340 return new InsetSpace(InsetSpace::PROTECTED);
342 return new InsetSpace(InsetSpace::THIN);
344 return new InsetSpace(InsetSpace::QUAD);
346 return new InsetSpace(InsetSpace::QQUAD);
347 if (name == "enspace")
348 return new InsetSpace(InsetSpace::ENSPACE);
349 if (name == "enskip")
350 return new InsetSpace(InsetSpace::ENSKIP);
351 if (name == "negthinspace")
352 return new InsetSpace(InsetSpace::NEGTHIN);
354 lyxerr << "LyX function 'space' needs an argument." << endl;
356 lyxerr << "Wrong argument for LyX function 'space'." << endl;
364 } catch (support::ExceptionMessage const & message) {
365 if (message.type_ == support::ErrorException) {
366 Alert::error(message.title_, message.details_);
367 LyX::cref().emergencyCleanup();
369 } else if (message.type_ == support::WarningException) {
370 Alert::warning(message.title_, message.details_);
380 Inset * readInset(Lexer & lex, Buffer const & buf)
383 if (lex.getString() != "\\begin_inset") {
384 lyxerr << "Buffer::readInset: Consistency check failed."
388 auto_ptr<Inset> inset;
390 TextClass const & tclass = buf.params().getTextClass();
393 string tmptok = lex.getString();
395 // test the different insets
397 //FIXME It would be better if we did not have this branch and could
398 //just do one massive switch for all insets. But at present, it's easier
399 //to do it this way, and we can't do the massive switch until the conversion
401 //Note that if we do want to do a single switch, we need to remove
402 //this "CommandInset" line---or replace it with a single "InsetType" line
403 //that would be used in all insets.
404 if (tmptok == "CommandInset") {
406 string const insetType = lex.getString();
407 lex.pushToken(insetType);
409 InsetCode const code = insetCode(insetType);
411 //FIXME If we do the one massive switch, we cannot do this here, since
412 //we do not know in advance that we're dealing with a command inset.
413 //Worst case, we could put it in each case below. Better, we could
414 //pass the lexer to the constructor and let the params be built there.
415 InsetCommandParams inscmd(code);
420 inset.reset(new InsetBibitem(inscmd));
423 inset.reset(new InsetBibtex(inscmd));
426 inset.reset(new InsetCitation(inscmd));
429 inset.reset(new InsetHyperlink(inscmd));
432 inset.reset(new InsetInclude(inscmd));
434 case INDEX_PRINT_CODE:
435 inset.reset(new InsetPrintIndex(inscmd));
438 inset.reset(new InsetLabel(inscmd));
441 inset.reset(new InsetNomencl(inscmd));
443 case NOMENCL_PRINT_CODE:
444 inset.reset(new InsetPrintNomencl(inscmd));
447 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
448 inset.reset(new InsetRef(inscmd, buf));
451 inset.reset(new InsetTOC(inscmd));
455 lyxerr << "unknown CommandInset '" << insetType
457 while (lex.isOK() && lex.getString() != "\\end_inset")
462 // FIXME This branch should be made to use inset codes as the preceding
463 // branch does. Unfortunately, that will take some doing. It requires
464 // converting the representation of the insets in LyX files so that they
465 // use the inset names listed in Inset.cpp. Then, as above, the inset names
466 // can be translated to inset codes using insetCode(). And the insets'
467 // write() routines should use insetName() rather than hardcoding it.
468 if (tmptok == "Quotes") {
469 inset.reset(new InsetQuotes);
470 } else if (tmptok == "External") {
471 inset.reset(new InsetExternal);
472 } else if (tmptok == "FormulaMacro") {
473 inset.reset(new MathMacroTemplate);
474 } else if (tmptok == "Formula") {
475 inset.reset(new InsetMathHull);
476 } else if (tmptok == "Graphics") {
477 inset.reset(new InsetGraphics);
478 } else if (tmptok == "Note") {
479 inset.reset(new InsetNote(buf.params(), tmptok));
480 } else if (tmptok == "Box") {
481 inset.reset(new InsetBox(buf.params(), tmptok));
482 } else if (tmptok == "Flex") {
484 string s = lex.getString();
485 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
486 inset.reset(new InsetFlex(buf.params(), il));
487 } else if (tmptok == "Branch") {
488 inset.reset(new InsetBranch(buf.params(),
489 InsetBranchParams()));
490 } else if (tmptok == "Environment") {
492 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
493 } else if (tmptok == "ERT") {
494 inset.reset(new InsetERT(buf.params()));
495 } else if (tmptok == "listings") {
496 inset.reset(new InsetListings(buf.params()));
497 } else if (tmptok == "InsetSpace") {
498 inset.reset(new InsetSpace);
499 } else if (tmptok == "Tabular") {
500 inset.reset(new InsetTabular(buf));
501 } else if (tmptok == "Text") {
502 inset.reset(new InsetText(buf.params()));
503 } else if (tmptok == "VSpace") {
504 inset.reset(new InsetVSpace);
505 } else if (tmptok == "Foot") {
506 inset.reset(new InsetFoot(buf.params()));
507 } else if (tmptok == "Marginal") {
508 inset.reset(new InsetMarginal(buf.params()));
509 } else if (tmptok == "OptArg") {
510 inset.reset(new InsetOptArg(buf.params()));
511 } else if (tmptok == "Float") {
513 string tmptok = lex.getString();
514 inset.reset(new InsetFloat(buf.params(), tmptok));
515 } else if (tmptok == "Wrap") {
517 string tmptok = lex.getString();
518 inset.reset(new InsetWrap(buf.params(), tmptok));
520 } else if (tmptok == "Theorem") {
521 inset.reset(new InsetList);
523 } else if (tmptok == "Caption") {
524 inset.reset(new InsetCaption(buf.params()));
525 } else if (tmptok == "Index") {
526 inset.reset(new InsetIndex(buf.params()));
527 } else if (tmptok == "FloatList") {
528 inset.reset(new InsetFloatList);
529 } else if (tmptok == "Info") {
530 inset.reset(new InsetInfo(buf.params()));
532 lyxerr << "unknown Inset type '" << tmptok
534 while (lex.isOK() && lex.getString() != "\\end_inset")
539 inset->read(buf, lex);
542 return inset.release();