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>
71 using namespace lyx::support;
75 namespace Alert = frontend::Alert;
78 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
80 BufferParams const & params = buf.params();
85 case LFUN_HFILL_INSERT:
86 return new InsetHFill;
88 case LFUN_LINE_INSERT:
91 case LFUN_NEWPAGE_INSERT:
92 return new InsetNewpage;
94 case LFUN_PAGEBREAK_INSERT:
95 return new InsetPagebreak;
97 case LFUN_CLEARPAGE_INSERT:
98 return new InsetClearPage;
100 case LFUN_CLEARDOUBLEPAGE_INSERT:
101 return new InsetClearDoublePage;
103 case LFUN_FLEX_INSERT: {
104 string s = cmd.getArg(0);
105 TextClass const & tclass = params.getTextClass();
106 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
107 return new InsetFlex(params, il);
110 case LFUN_NOTE_INSERT: {
111 string arg = cmd.getArg(0);
114 return new InsetNote(params, arg);
117 case LFUN_BOX_INSERT: {
118 string arg = cmd.getArg(0);
121 return new InsetBox(params, arg);
124 case LFUN_BRANCH_INSERT: {
125 docstring arg = cmd.argument();
127 arg = from_ascii("none");
128 return new InsetBranch(params, InsetBranchParams(arg));
131 case LFUN_ERT_INSERT:
132 return new InsetERT(params);
134 case LFUN_LISTING_INSERT:
135 return new InsetListings(params);
137 case LFUN_FOOTNOTE_INSERT:
138 return new InsetFoot(params);
140 case LFUN_MARGINALNOTE_INSERT:
141 return new InsetMarginal(params);
143 case LFUN_OPTIONAL_INSERT:
144 return new InsetOptArg(params);
146 case LFUN_BIBITEM_INSERT:
147 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
149 case LFUN_FLOAT_INSERT: {
150 // check if the float type exists
151 string const argument = to_utf8(cmd.argument());
152 if (params.getTextClass().floats().typeExist(argument))
153 return new InsetFloat(params, argument);
154 lyxerr << "Non-existent float type: " << argument << endl;
157 case LFUN_FLOAT_WIDE_INSERT: {
158 // check if the float type exists
159 string const argument = to_utf8(cmd.argument());
160 if (params.getTextClass().floats().typeExist(argument)) {
161 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
162 p->wide(true, params);
165 lyxerr << "Non-existent float type: " << argument << endl;
169 case LFUN_WRAP_INSERT: {
170 string const argument = to_utf8(cmd.argument());
171 if (argument == "figure" || argument == "table")
172 return new InsetWrap(params, argument);
173 lyxerr << "Non-existent wrapfig type: " << argument << endl;
177 case LFUN_INDEX_INSERT:
178 return new InsetIndex(params);
180 case LFUN_NOMENCL_INSERT: {
181 InsetCommandParams icp(NOMENCL_CODE);
182 icp["symbol"] = cmd.argument();
183 return new InsetNomencl(icp);
186 case LFUN_TABULAR_INSERT: {
187 if (cmd.argument().empty())
189 istringstream ss(to_utf8(cmd.argument()));
196 return new InsetTabular(buf, r, c);
199 case LFUN_CAPTION_INSERT: {
200 auto_ptr<InsetCaption> inset(new InsetCaption(params));
201 inset->setAutoBreakRows(true);
202 inset->setDrawFrame(true);
203 inset->setFrameColor(Color_captionframe);
204 return inset.release();
207 case LFUN_INDEX_PRINT:
208 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
210 case LFUN_NOMENCL_PRINT:
211 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
213 case LFUN_TOC_INSERT:
214 return new InsetTOC(InsetCommandParams(TOC_CODE));
216 case LFUN_ENVIRONMENT_INSERT:
217 return new InsetEnvironment(params, cmd.argument());
219 case LFUN_INFO_INSERT:
220 return new InsetInfo(params, to_utf8(cmd.argument()));
222 case LFUN_THEOREM_INSERT:
223 return new InsetTheorem;
226 case LFUN_INSET_INSERT: {
227 string const name = cmd.getArg(0);
228 InsetCode code = insetCode(name);
231 lyxerr << "No such inset '" << name << "'.";
235 InsetCommandParams icp(code);
236 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
237 return new InsetBibitem(icp);
241 InsetCommandParams icp(code);
242 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
243 return new InsetBibtex(icp);
247 InsetCommandParams icp(code);
248 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
249 return new InsetCitation(icp);
253 InsetCollapsable::CollapseStatus st;
254 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
255 return new InsetERT(params, st);
258 case LISTINGS_CODE: {
259 InsetListingsParams par;
260 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
261 return new InsetListings(params, par);
264 case EXTERNAL_CODE: {
265 InsetExternalParams iep;
266 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
267 auto_ptr<InsetExternal> inset(new InsetExternal);
268 inset->setParams(iep, buf);
269 return inset.release();
272 case GRAPHICS_CODE: {
273 InsetGraphicsParams igp;
274 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
275 auto_ptr<InsetGraphics> inset(new InsetGraphics);
276 inset->setParams(igp);
277 return inset.release();
280 case HYPERLINK_CODE: {
281 InsetCommandParams icp(code);
282 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
283 return new InsetHyperlink(icp);
287 InsetCommandParams icp(code);
288 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
289 return new InsetInclude(icp);
293 return new InsetIndex(params);
296 InsetCommandParams icp(code);
297 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
298 return new InsetNomencl(icp);
302 InsetCommandParams icp(code);
303 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
304 return new InsetLabel(icp);
308 InsetCommandParams icp(code);
309 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
310 return new InsetRef(icp, buf);
314 InsetCommandParams icp(code);
315 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
316 return new InsetTOC(icp);
321 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
322 return new InsetVSpace(vspace);
326 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
331 } //end LFUN_INSET_INSERT
333 case LFUN_SPACE_INSERT: {
334 string const name = to_utf8(cmd.argument());
335 if (name == "normal")
336 return new InsetSpace(InsetSpace::NORMAL);
337 if (name == "protected")
338 return new InsetSpace(InsetSpace::PROTECTED);
340 return new InsetSpace(InsetSpace::THIN);
342 return new InsetSpace(InsetSpace::QUAD);
344 return new InsetSpace(InsetSpace::QQUAD);
345 if (name == "enspace")
346 return new InsetSpace(InsetSpace::ENSPACE);
347 if (name == "enskip")
348 return new InsetSpace(InsetSpace::ENSKIP);
349 if (name == "negthinspace")
350 return new InsetSpace(InsetSpace::NEGTHIN);
352 lyxerr << "LyX function 'space' needs an argument." << endl;
354 lyxerr << "Wrong argument for LyX function 'space'." << endl;
362 } catch (ExceptionMessage const & message) {
363 if (message.type_ == ErrorException) {
364 // This should never happen!
365 Alert::error(message.title_, message.details_);
367 } else if (message.type_ == WarningException) {
368 Alert::warning(message.title_, message.details_);
378 Inset * readInset(Lexer & lex, Buffer const & buf)
381 if (lex.getString() != "\\begin_inset") {
382 lyxerr << "Buffer::readInset: Consistency check failed."
386 auto_ptr<Inset> inset;
388 TextClass const & tclass = buf.params().getTextClass();
391 string tmptok = lex.getString();
393 // test the different insets
395 //FIXME It would be better if we did not have this branch and could
396 //just do one massive switch for all insets. But at present, it's easier
397 //to do it this way, and we can't do the massive switch until the conversion
399 //Note that if we do want to do a single switch, we need to remove
400 //this "CommandInset" line---or replace it with a single "InsetType" line
401 //that would be used in all insets.
402 if (tmptok == "CommandInset") {
404 string const insetType = lex.getString();
405 lex.pushToken(insetType);
407 InsetCode const code = insetCode(insetType);
409 //FIXME If we do the one massive switch, we cannot do this here, since
410 //we do not know in advance that we're dealing with a command inset.
411 //Worst case, we could put it in each case below. Better, we could
412 //pass the lexer to the constructor and let the params be built there.
413 InsetCommandParams inscmd(code);
418 inset.reset(new InsetBibitem(inscmd));
421 inset.reset(new InsetBibtex(inscmd));
424 inset.reset(new InsetCitation(inscmd));
427 inset.reset(new InsetHyperlink(inscmd));
430 inset.reset(new InsetInclude(inscmd));
432 case INDEX_PRINT_CODE:
433 inset.reset(new InsetPrintIndex(inscmd));
436 inset.reset(new InsetLabel(inscmd));
439 inset.reset(new InsetNomencl(inscmd));
441 case NOMENCL_PRINT_CODE:
442 inset.reset(new InsetPrintNomencl(inscmd));
445 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
446 inset.reset(new InsetRef(inscmd, buf));
449 inset.reset(new InsetTOC(inscmd));
453 lyxerr << "unknown CommandInset '" << insetType
455 while (lex.isOK() && lex.getString() != "\\end_inset")
460 // FIXME This branch should be made to use inset codes as the preceding
461 // branch does. Unfortunately, that will take some doing. It requires
462 // converting the representation of the insets in LyX files so that they
463 // use the inset names listed in Inset.cpp. Then, as above, the inset names
464 // can be translated to inset codes using insetCode(). And the insets'
465 // write() routines should use insetName() rather than hardcoding it.
466 if (tmptok == "Quotes") {
467 inset.reset(new InsetQuotes);
468 } else if (tmptok == "External") {
469 inset.reset(new InsetExternal);
470 } else if (tmptok == "FormulaMacro") {
471 inset.reset(new MathMacroTemplate);
472 } else if (tmptok == "Formula") {
473 inset.reset(new InsetMathHull);
474 } else if (tmptok == "Graphics") {
475 inset.reset(new InsetGraphics);
476 } else if (tmptok == "Note") {
477 inset.reset(new InsetNote(buf.params(), tmptok));
478 } else if (tmptok == "Box") {
479 inset.reset(new InsetBox(buf.params(), tmptok));
480 } else if (tmptok == "Flex") {
482 string s = lex.getString();
483 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
484 inset.reset(new InsetFlex(buf.params(), il));
485 } else if (tmptok == "Branch") {
486 inset.reset(new InsetBranch(buf.params(),
487 InsetBranchParams()));
488 } else if (tmptok == "Environment") {
490 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
491 } else if (tmptok == "ERT") {
492 inset.reset(new InsetERT(buf.params()));
493 } else if (tmptok == "listings") {
494 inset.reset(new InsetListings(buf.params()));
495 } else if (tmptok == "InsetSpace") {
496 inset.reset(new InsetSpace);
497 } else if (tmptok == "Tabular") {
498 inset.reset(new InsetTabular(buf));
499 } else if (tmptok == "Text") {
500 inset.reset(new InsetText(buf.params()));
501 } else if (tmptok == "VSpace") {
502 inset.reset(new InsetVSpace);
503 } else if (tmptok == "Foot") {
504 inset.reset(new InsetFoot(buf.params()));
505 } else if (tmptok == "Marginal") {
506 inset.reset(new InsetMarginal(buf.params()));
507 } else if (tmptok == "OptArg") {
508 inset.reset(new InsetOptArg(buf.params()));
509 } else if (tmptok == "Float") {
511 string tmptok = lex.getString();
512 inset.reset(new InsetFloat(buf.params(), tmptok));
513 } else if (tmptok == "Wrap") {
515 string tmptok = lex.getString();
516 inset.reset(new InsetWrap(buf.params(), tmptok));
518 } else if (tmptok == "Theorem") {
519 inset.reset(new InsetList);
521 } else if (tmptok == "Caption") {
522 inset.reset(new InsetCaption(buf.params()));
523 } else if (tmptok == "Index") {
524 inset.reset(new InsetIndex(buf.params()));
525 } else if (tmptok == "FloatList") {
526 inset.reset(new InsetFloatList);
527 } else if (tmptok == "Info") {
528 inset.reset(new InsetInfo(buf.params()));
530 lyxerr << "unknown Inset type '" << tmptok
532 while (lex.isOK() && lex.getString() != "\\end_inset")
537 inset->read(buf, lex);
540 return inset.release();