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->setParams(iep);
255 return inset.release();
258 case GRAPHICS_CODE: {
259 InsetGraphicsParams igp;
260 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
261 auto_ptr<InsetGraphics> inset(new InsetGraphics);
262 inset->setParams(igp);
263 return inset.release();
266 case HYPERLINK_CODE: {
267 InsetCommandParams icp(code);
268 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
269 return new InsetHyperlink(icp);
273 InsetCommandParams icp(code);
274 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
275 return new InsetInclude(icp);
279 return new InsetIndex(buf);
282 InsetCommandParams icp(code);
283 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
284 return new InsetNomencl(icp);
288 InsetCommandParams icp(code);
289 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
290 return new InsetLabel(icp);
294 InsetCommandParams icp(code);
295 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
296 return new InsetRef(buf, icp);
300 InsetSpaceParams isp;
301 InsetSpaceMailer::string2params(to_utf8(cmd.argument()), isp);
302 return new InsetSpace(isp);
306 InsetCommandParams icp(code);
307 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
308 return new InsetTOC(icp);
313 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
314 return new InsetVSpace(vspace);
318 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
323 } //end LFUN_INSET_INSERT
325 case LFUN_SPACE_INSERT: {
326 string const name = cmd.getArg(0);
327 string const len = cmd.getArg(1);
329 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
332 InsetSpaceParams isp;
333 if (name == "normal")
334 isp.kind = InsetSpaceParams::NORMAL;
335 else if (name == "protected")
336 isp.kind = InsetSpaceParams::PROTECTED;
337 else if (name == "thin")
338 isp.kind = InsetSpaceParams::THIN;
339 else if (name == "quad")
340 isp.kind = InsetSpaceParams::QUAD;
341 else if (name == "qquad")
342 isp.kind = InsetSpaceParams::QQUAD;
343 else if (name == "enspace")
344 isp.kind = InsetSpaceParams::ENSPACE;
345 else if (name == "enskip")
346 isp.kind = InsetSpaceParams::ENSKIP;
347 else if (name == "negthinspace")
348 isp.kind = InsetSpaceParams::NEGTHIN;
349 else if (name == "hfill")
350 isp.kind = InsetSpaceParams::HFILL;
351 else if (name == "hfill*")
352 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
353 else if (name == "dotfill")
354 isp.kind = InsetSpaceParams::DOTFILL;
355 else if (name == "hrulefill")
356 isp.kind = InsetSpaceParams::HRULEFILL;
357 else if (name == "hspace") {
358 if (len.empty() || !isValidLength(len)) {
359 lyxerr << "LyX function 'space-insert hspace' "
360 << "needs a valid length argument." << endl;
363 isp.kind = InsetSpaceParams::CUSTOM;
364 isp.length = Length(len);
366 else if (name == "hspace*") {
367 if (len.empty() || !isValidLength(len)) {
368 lyxerr << "LyX function 'space-insert hspace*' "
369 << "needs a valid length argument." << endl;
372 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
373 isp.length = Length(len);
376 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
379 return new InsetSpace(isp);
387 } catch (ExceptionMessage const & message) {
388 if (message.type_ == ErrorException) {
389 // This should never happen!
390 Alert::error(message.title_, message.details_);
392 } else if (message.type_ == WarningException) {
393 Alert::warning(message.title_, message.details_);
402 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
404 Inset * inset = createInsetHelper(buf, cmd);
406 inset->setBuffer(buf);
410 Inset * readInset(Lexer & lex, Buffer const & buf)
413 if (lex.getString() != "\\begin_inset") {
414 lyxerr << "Buffer::readInset: Consistency check failed."
418 auto_ptr<Inset> inset;
421 string tmptok = lex.getString();
423 // test the different insets
425 //FIXME It would be better if we did not have this branch and could
426 //just do one massive switch for all insets. But at present, it's easier
427 //to do it this way, and we can't do the massive switch until the conversion
429 //Note that if we do want to do a single switch, we need to remove
430 //this "CommandInset" line---or replace it with a single "InsetType" line
431 //that would be used in all insets.
432 if (tmptok == "CommandInset") {
434 string const insetType = lex.getString();
435 lex.pushToken(insetType);
437 InsetCode const code = insetCode(insetType);
439 //FIXME If we do the one massive switch, we cannot do this here, since
440 //we do not know in advance that we're dealing with a command inset.
441 //Worst case, we could put it in each case below. Better, we could
442 //pass the lexer to the constructor and let the params be built there.
443 InsetCommandParams inscmd(code);
448 inset.reset(new InsetBibitem(inscmd));
451 inset.reset(new InsetBibtex(inscmd));
454 inset.reset(new InsetCitation(inscmd));
457 inset.reset(new InsetHyperlink(inscmd));
460 inset.reset(new InsetInclude(inscmd));
462 case INDEX_PRINT_CODE:
463 inset.reset(new InsetPrintIndex(inscmd));
466 inset.reset(new InsetLabel(inscmd));
469 inset.reset(new InsetNomencl(inscmd));
471 case NOMENCL_PRINT_CODE:
472 inset.reset(new InsetPrintNomencl(inscmd));
475 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
476 inset.reset(new InsetRef(buf, inscmd));
479 inset.reset(new InsetTOC(inscmd));
483 lyxerr << "unknown CommandInset '" << insetType
485 while (lex.isOK() && lex.getString() != "\\end_inset")
489 inset->setBuffer(const_cast<Buffer &>(buf));
491 // FIXME This branch should be made to use inset codes as the preceding
492 // branch does. Unfortunately, that will take some doing. It requires
493 // converting the representation of the insets in LyX files so that they
494 // use the inset names listed in Inset.cpp. Then, as above, the inset names
495 // can be translated to inset codes using insetCode(). And the insets'
496 // write() routines should use insetName() rather than hardcoding it.
497 if (tmptok == "Quotes") {
498 inset.reset(new InsetQuotes);
499 } else if (tmptok == "External") {
500 inset.reset(new InsetExternal);
501 } else if (tmptok == "FormulaMacro") {
502 inset.reset(new MathMacroTemplate);
503 } else if (tmptok == "Formula") {
504 inset.reset(new InsetMathHull);
505 } else if (tmptok == "Graphics") {
506 inset.reset(new InsetGraphics);
507 } else if (tmptok == "Note") {
508 inset.reset(new InsetNote(buf, tmptok));
509 } else if (tmptok == "Box") {
510 inset.reset(new InsetBox(buf, tmptok));
511 } else if (tmptok == "Flex") {
513 string s = lex.getString();
514 inset.reset(new InsetFlex(buf,
515 buf.params().documentClassPtr(), s));
516 } else if (tmptok == "Branch") {
517 inset.reset(new InsetBranch(buf,
518 InsetBranchParams()));
519 } else if (tmptok == "Environment") {
521 inset.reset(new InsetEnvironment(buf, lex.getDocString()));
522 } else if (tmptok == "ERT") {
523 inset.reset(new InsetERT(buf));
524 } else if (tmptok == "listings") {
525 inset.reset(new InsetListings(buf));
526 } else if (tmptok == "Space") {
527 inset.reset(new InsetSpace);
528 } else if (tmptok == "Tabular") {
529 inset.reset(new InsetTabular(buf));
530 } else if (tmptok == "Text") {
531 inset.reset(new InsetText(buf));
532 } else if (tmptok == "VSpace") {
533 inset.reset(new InsetVSpace);
534 } else if (tmptok == "Foot") {
535 inset.reset(new InsetFoot(buf));
536 } else if (tmptok == "Marginal") {
537 inset.reset(new InsetMarginal(buf));
538 } else if (tmptok == "OptArg") {
539 inset.reset(new InsetOptArg(buf));
540 } else if (tmptok == "Float") {
542 string tmptok = lex.getString();
543 inset.reset(new InsetFloat(buf, tmptok));
544 } else if (tmptok == "Wrap") {
546 string tmptok = lex.getString();
547 inset.reset(new InsetWrap(buf, tmptok));
549 } else if (tmptok == "Theorem") {
550 inset.reset(new InsetList);
552 } else if (tmptok == "Caption") {
553 inset.reset(new InsetCaption(buf));
554 } else if (tmptok == "Index") {
555 inset.reset(new InsetIndex(buf));
556 } else if (tmptok == "FloatList") {
557 inset.reset(new InsetFloatList);
558 } else if (tmptok == "Info") {
559 inset.reset(new InsetInfo(buf));
561 lyxerr << "unknown Inset type '" << tmptok
563 while (lex.isOK() && lex.getString() != "\\end_inset")
568 // Set the buffer reference for proper parsing of some insets
569 // (InsetCollapsable for example)
570 inset->setBuffer(const_cast<Buffer &>(buf));
572 // Set again the buffer for insets that are created inside this inset
573 // (InsetMathHull for example).
574 inset->setBuffer(const_cast<Buffer &>(buf));
576 return inset.release();