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;
79 using support::compare_ascii_no_case;
82 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
84 BufferParams const & params = buf.params();
89 case LFUN_HFILL_INSERT:
90 return new InsetHFill;
92 case LFUN_LINE_INSERT:
95 case LFUN_NEWPAGE_INSERT:
96 return new InsetNewpage;
98 case LFUN_PAGEBREAK_INSERT:
99 return new InsetPagebreak;
101 case LFUN_CLEARPAGE_INSERT:
102 return new InsetClearPage;
104 case LFUN_CLEARDOUBLEPAGE_INSERT:
105 return new InsetClearDoublePage;
107 case LFUN_FLEX_INSERT: {
108 string s = cmd.getArg(0);
109 TextClass const & tclass = params.getTextClass();
110 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
111 return new InsetFlex(params, il);
114 case LFUN_NOTE_INSERT: {
115 string arg = cmd.getArg(0);
118 return new InsetNote(params, arg);
121 case LFUN_BOX_INSERT: {
122 string arg = cmd.getArg(0);
125 return new InsetBox(params, arg);
128 case LFUN_BRANCH_INSERT: {
129 docstring arg = cmd.argument();
131 arg = from_ascii("none");
132 return new InsetBranch(params, InsetBranchParams(arg));
135 case LFUN_ERT_INSERT:
136 return new InsetERT(params);
138 case LFUN_LISTING_INSERT:
139 return new InsetListings(params);
141 case LFUN_FOOTNOTE_INSERT:
142 return new InsetFoot(params);
144 case LFUN_MARGINALNOTE_INSERT:
145 return new InsetMarginal(params);
147 case LFUN_OPTIONAL_INSERT:
148 return new InsetOptArg(params);
150 case LFUN_BIBITEM_INSERT:
151 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
153 case LFUN_FLOAT_INSERT: {
154 // check if the float type exists
155 string const argument = to_utf8(cmd.argument());
156 if (params.getTextClass().floats().typeExist(argument))
157 return new InsetFloat(params, argument);
158 lyxerr << "Non-existent float type: " << argument << endl;
161 case LFUN_FLOAT_WIDE_INSERT: {
162 // check if the float type exists
163 string const argument = to_utf8(cmd.argument());
164 if (params.getTextClass().floats().typeExist(argument)) {
165 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
166 p->wide(true, params);
169 lyxerr << "Non-existent float type: " << argument << endl;
173 case LFUN_WRAP_INSERT: {
174 string const argument = to_utf8(cmd.argument());
175 if (argument == "figure" || argument == "table")
176 return new InsetWrap(params, argument);
177 lyxerr << "Non-existent wrapfig type: " << argument << endl;
181 case LFUN_INDEX_INSERT:
182 return new InsetIndex(params);
184 case LFUN_NOMENCL_INSERT: {
185 InsetCommandParams icp(NOMENCL_CODE);
186 icp["symbol"] = cmd.argument();
187 return new InsetNomencl(icp);
190 case LFUN_TABULAR_INSERT: {
191 if (cmd.argument().empty())
193 std::istringstream ss(to_utf8(cmd.argument()));
200 return new InsetTabular(buf, r, c);
203 case LFUN_CAPTION_INSERT: {
204 auto_ptr<InsetCaption> inset(new InsetCaption(params));
205 inset->setAutoBreakRows(true);
206 inset->setDrawFrame(true);
207 inset->setFrameColor(Color_captionframe);
208 return inset.release();
211 case LFUN_INDEX_PRINT:
212 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
214 case LFUN_NOMENCL_PRINT:
215 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
217 case LFUN_TOC_INSERT:
218 return new InsetTOC(InsetCommandParams(TOC_CODE));
220 case LFUN_ENVIRONMENT_INSERT:
221 return new InsetEnvironment(params, cmd.argument());
223 case LFUN_INFO_INSERT:
224 return new InsetInfo(params, to_utf8(cmd.argument()));
226 case LFUN_THEOREM_INSERT:
227 return new InsetTheorem;
230 case LFUN_INSET_INSERT: {
231 string const name = cmd.getArg(0);
232 InsetCode code = insetCode(name);
235 lyxerr << "No such inset '" << name << "'.";
239 InsetCommandParams icp(code);
240 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
241 return new InsetBibitem(icp);
245 InsetCommandParams icp(code);
246 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
247 return new InsetBibtex(icp);
251 InsetCommandParams icp(code);
252 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
253 return new InsetCitation(icp);
257 InsetCollapsable::CollapseStatus st;
258 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
259 return new InsetERT(params, st);
262 case LISTINGS_CODE: {
263 InsetListingsParams par;
264 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
265 return new InsetListings(params, par);
268 case EXTERNAL_CODE: {
269 InsetExternalParams iep;
270 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
271 auto_ptr<InsetExternal> inset(new InsetExternal);
272 inset->setParams(iep, buf);
273 return inset.release();
276 case GRAPHICS_CODE: {
277 InsetGraphicsParams igp;
278 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
279 auto_ptr<InsetGraphics> inset(new InsetGraphics);
280 inset->setParams(igp);
281 return inset.release();
284 case HYPERLINK_CODE: {
285 InsetCommandParams icp(code);
286 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
287 return new InsetHyperlink(icp);
291 InsetCommandParams icp(code);
292 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
293 return new InsetInclude(icp);
297 return new InsetIndex(params);
300 InsetCommandParams icp(code);
301 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
302 return new InsetNomencl(icp);
306 InsetCommandParams icp(code);
307 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
308 return new InsetLabel(icp);
312 InsetCommandParams icp(code);
313 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
314 return new InsetRef(icp, buf);
318 InsetCommandParams icp(code);
319 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
320 return new InsetTOC(icp);
325 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
326 return new InsetVSpace(vspace);
330 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
335 } //end LFUN_INSET_INSERT
337 case LFUN_SPACE_INSERT: {
338 string const name = to_utf8(cmd.argument());
339 if (name == "normal")
340 return new InsetSpace(InsetSpace::NORMAL);
341 if (name == "protected")
342 return new InsetSpace(InsetSpace::PROTECTED);
344 return new InsetSpace(InsetSpace::THIN);
346 return new InsetSpace(InsetSpace::QUAD);
348 return new InsetSpace(InsetSpace::QQUAD);
349 if (name == "enspace")
350 return new InsetSpace(InsetSpace::ENSPACE);
351 if (name == "enskip")
352 return new InsetSpace(InsetSpace::ENSKIP);
353 if (name == "negthinspace")
354 return new InsetSpace(InsetSpace::NEGTHIN);
356 lyxerr << "LyX function 'space' needs an argument." << endl;
358 lyxerr << "Wrong argument for LyX function 'space'." << endl;
366 } catch (support::ExceptionMessage const & message) {
367 if (message.type_ == support::ErrorException) {
368 Alert::error(message.title_, message.details_);
369 LyX::cref().emergencyCleanup();
371 } else if (message.type_ == support::WarningException) {
372 Alert::warning(message.title_, message.details_);
382 Inset * readInset(Lexer & lex, Buffer const & buf)
385 if (lex.getString() != "\\begin_inset") {
386 lyxerr << "Buffer::readInset: Consistency check failed."
390 auto_ptr<Inset> inset;
392 TextClass const & tclass = buf.params().getTextClass();
395 string tmptok = lex.getString();
397 // test the different insets
399 //FIXME It would be better if we did not have this branch and could
400 //just do one massive switch for all insets. But at present, it's easier
401 //to do it this way, and we can't do the massive switch until the conversion
403 //Note that if we do want to do a single switch, we need to remove
404 //this "CommandInset" line---or replace it with a single "InsetType" line
405 //that would be used in all insets.
406 if (tmptok == "CommandInset") {
408 string const insetType = lex.getString();
409 lex.pushToken(insetType);
411 InsetCode const code = insetCode(insetType);
413 //FIXME If we do the one massive switch, we cannot do this here, since
414 //we do not know in advance that we're dealing with a command inset.
415 //Worst case, we could put it in each case below. Better, we could
416 //pass the lexer to the constructor and let the params be built there.
417 InsetCommandParams inscmd(code);
422 inset.reset(new InsetBibitem(inscmd));
425 inset.reset(new InsetBibtex(inscmd));
428 inset.reset(new InsetCitation(inscmd));
431 inset.reset(new InsetHyperlink(inscmd));
434 inset.reset(new InsetInclude(inscmd));
436 case INDEX_PRINT_CODE:
437 inset.reset(new InsetPrintIndex(inscmd));
440 inset.reset(new InsetLabel(inscmd));
443 inset.reset(new InsetNomencl(inscmd));
445 case NOMENCL_PRINT_CODE:
446 inset.reset(new InsetPrintNomencl(inscmd));
449 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
450 inset.reset(new InsetRef(inscmd, buf));
453 inset.reset(new InsetTOC(inscmd));
457 lyxerr << "unknown CommandInset '" << insetType
459 while (lex.isOK() && lex.getString() != "\\end_inset")
464 // FIXME This branch should be made to use inset codes as the preceding
465 // branch does. Unfortunately, that will take some doing. It requires
466 // converting the representation of the insets in LyX files so that they
467 // use the inset names listed in Inset.cpp. Then, as above, the inset names
468 // can be translated to inset codes using insetCode(). And the insets'
469 // write() routines should use insetName() rather than hardcoding it.
470 if (tmptok == "Quotes") {
471 inset.reset(new InsetQuotes);
472 } else if (tmptok == "External") {
473 inset.reset(new InsetExternal);
474 } else if (tmptok == "FormulaMacro") {
475 inset.reset(new MathMacroTemplate);
476 } else if (tmptok == "Formula") {
477 inset.reset(new InsetMathHull);
478 } else if (tmptok == "Graphics") {
479 inset.reset(new InsetGraphics);
480 } else if (tmptok == "Note") {
481 inset.reset(new InsetNote(buf.params(), tmptok));
482 } else if (tmptok == "Box") {
483 inset.reset(new InsetBox(buf.params(), tmptok));
484 } else if (tmptok == "Flex") {
486 string s = lex.getString();
487 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
488 inset.reset(new InsetFlex(buf.params(), il));
489 } else if (tmptok == "Branch") {
490 inset.reset(new InsetBranch(buf.params(),
491 InsetBranchParams()));
492 } else if (tmptok == "Environment") {
494 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
495 } else if (tmptok == "ERT") {
496 inset.reset(new InsetERT(buf.params()));
497 } else if (tmptok == "listings") {
498 inset.reset(new InsetListings(buf.params()));
499 } else if (tmptok == "InsetSpace") {
500 inset.reset(new InsetSpace);
501 } else if (tmptok == "Tabular") {
502 inset.reset(new InsetTabular(buf));
503 } else if (tmptok == "Text") {
504 inset.reset(new InsetText(buf.params()));
505 } else if (tmptok == "VSpace") {
506 inset.reset(new InsetVSpace);
507 } else if (tmptok == "Foot") {
508 inset.reset(new InsetFoot(buf.params()));
509 } else if (tmptok == "Marginal") {
510 inset.reset(new InsetMarginal(buf.params()));
511 } else if (tmptok == "OptArg") {
512 inset.reset(new InsetOptArg(buf.params()));
513 } else if (tmptok == "Float") {
515 string tmptok = lex.getString();
516 inset.reset(new InsetFloat(buf.params(), tmptok));
517 } else if (tmptok == "Wrap") {
519 string tmptok = lex.getString();
520 inset.reset(new InsetWrap(buf.params(), tmptok));
522 } else if (tmptok == "Theorem") {
523 inset.reset(new InsetList);
525 } else if (tmptok == "Caption") {
526 inset.reset(new InsetCaption(buf.params()));
527 } else if (tmptok == "Index") {
528 inset.reset(new InsetIndex(buf.params()));
529 } else if (tmptok == "FloatList") {
530 inset.reset(new InsetFloatList);
531 } else if (tmptok == "Info") {
532 inset.reset(new InsetInfo(buf.params()));
534 lyxerr << "unknown Inset type '" << tmptok
536 while (lex.isOK() && lex.getString() != "\\end_inset")
541 inset->read(buf, lex);
544 return inset.release();