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"
18 #include "FloatList.h"
19 #include "FuncRequest.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/InsetPagebreak.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/lstrings.h"
63 #include "support/ExceptionMessage.h"
65 #include <boost/assert.hpp>
66 #include <boost/current_function.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_PAGEBREAK_INSERT:
96 return new InsetPagebreak;
98 case LFUN_CLEARPAGE_INSERT:
99 return new InsetClearPage;
101 case LFUN_CLEARDOUBLEPAGE_INSERT:
102 return new InsetClearDoublePage;
104 case LFUN_FLEX_INSERT: {
105 string s = cmd.getArg(0);
106 TextClass const & tclass = params.getTextClass();
107 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
108 return new InsetFlex(params, il);
111 case LFUN_NOTE_INSERT: {
112 string arg = cmd.getArg(0);
115 return new InsetNote(params, arg);
118 case LFUN_BOX_INSERT: {
119 string arg = cmd.getArg(0);
122 return new InsetBox(params, arg);
125 case LFUN_BRANCH_INSERT: {
126 docstring arg = cmd.argument();
128 arg = from_ascii("none");
129 return new InsetBranch(params, InsetBranchParams(arg));
132 case LFUN_ERT_INSERT:
133 return new InsetERT(params);
135 case LFUN_LISTING_INSERT:
136 return new InsetListings(params);
138 case LFUN_FOOTNOTE_INSERT:
139 return new InsetFoot(params);
141 case LFUN_MARGINALNOTE_INSERT:
142 return new InsetMarginal(params);
144 case LFUN_OPTIONAL_INSERT:
145 return new InsetOptArg(params);
147 case LFUN_BIBITEM_INSERT:
148 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
150 case LFUN_FLOAT_INSERT: {
151 // check if the float type exists
152 string const argument = to_utf8(cmd.argument());
153 if (params.getTextClass().floats().typeExist(argument))
154 return new InsetFloat(params, argument);
155 lyxerr << "Non-existent float type: " << argument << endl;
158 case LFUN_FLOAT_WIDE_INSERT: {
159 // check if the float type exists
160 string const argument = to_utf8(cmd.argument());
161 if (params.getTextClass().floats().typeExist(argument)) {
162 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
163 p->wide(true, params);
166 lyxerr << "Non-existent float type: " << argument << endl;
170 case LFUN_WRAP_INSERT: {
171 string const argument = to_utf8(cmd.argument());
172 if (argument == "figure" || argument == "table")
173 return new InsetWrap(params, argument);
174 lyxerr << "Non-existent wrapfig type: " << argument << endl;
178 case LFUN_INDEX_INSERT:
179 return new InsetIndex(params);
181 case LFUN_NOMENCL_INSERT: {
182 InsetCommandParams icp(NOMENCL_CODE);
183 icp["symbol"] = cmd.argument();
184 return new InsetNomencl(icp);
187 case LFUN_TABULAR_INSERT: {
188 if (cmd.argument().empty())
190 std::istringstream ss(to_utf8(cmd.argument()));
197 return new InsetTabular(buf, r, c);
200 case LFUN_CAPTION_INSERT: {
201 auto_ptr<InsetCaption> inset(new InsetCaption(params));
202 inset->setAutoBreakRows(true);
203 inset->setDrawFrame(true);
204 inset->setFrameColor(Color_captionframe);
205 return inset.release();
208 case LFUN_INDEX_PRINT:
209 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
211 case LFUN_NOMENCL_PRINT:
212 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
214 case LFUN_TOC_INSERT:
215 return new InsetTOC(InsetCommandParams(TOC_CODE));
217 case LFUN_ENVIRONMENT_INSERT:
218 return new InsetEnvironment(params, cmd.argument());
220 case LFUN_INFO_INSERT:
221 return new InsetInfo(params, to_utf8(cmd.argument()));
223 case LFUN_THEOREM_INSERT:
224 return new InsetTheorem;
227 case LFUN_INSET_INSERT: {
228 string const name = cmd.getArg(0);
229 InsetCode code = insetCode(name);
232 lyxerr << "No such inset '" << name << "'.";
236 InsetCommandParams icp(code);
237 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
238 return new InsetBibitem(icp);
242 InsetCommandParams icp(code);
243 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
244 return new InsetBibtex(icp);
248 InsetCommandParams icp(code);
249 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
250 return new InsetCitation(icp);
254 InsetCollapsable::CollapseStatus st;
255 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
256 return new InsetERT(params, st);
259 case LISTINGS_CODE: {
260 InsetListingsParams par;
261 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
262 return new InsetListings(params, par);
265 case EXTERNAL_CODE: {
266 InsetExternalParams iep;
267 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
268 auto_ptr<InsetExternal> inset(new InsetExternal);
269 inset->setParams(iep, buf);
270 return inset.release();
273 case GRAPHICS_CODE: {
274 InsetGraphicsParams igp;
275 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
276 auto_ptr<InsetGraphics> inset(new InsetGraphics);
277 inset->setParams(igp);
278 return inset.release();
281 case HYPERLINK_CODE: {
282 InsetCommandParams icp(code);
283 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
284 return new InsetHyperlink(icp);
288 InsetCommandParams icp(code);
289 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
290 return new InsetInclude(icp);
294 return new InsetIndex(params);
297 InsetCommandParams icp(code);
298 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
299 return new InsetNomencl(icp);
303 InsetCommandParams icp(code);
304 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
305 return new InsetLabel(icp);
309 InsetCommandParams icp(code);
310 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
311 return new InsetRef(icp, buf);
315 InsetCommandParams icp(code);
316 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
317 return new InsetTOC(icp);
322 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
323 return new InsetVSpace(vspace);
327 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
332 } //end LFUN_INSET_INSERT
334 case LFUN_SPACE_INSERT: {
335 string const name = to_utf8(cmd.argument());
336 if (name == "normal")
337 return new InsetSpace(InsetSpace::NORMAL);
338 if (name == "protected")
339 return new InsetSpace(InsetSpace::PROTECTED);
341 return new InsetSpace(InsetSpace::THIN);
343 return new InsetSpace(InsetSpace::QUAD);
345 return new InsetSpace(InsetSpace::QQUAD);
346 if (name == "enspace")
347 return new InsetSpace(InsetSpace::ENSPACE);
348 if (name == "enskip")
349 return new InsetSpace(InsetSpace::ENSKIP);
350 if (name == "negthinspace")
351 return new InsetSpace(InsetSpace::NEGTHIN);
353 lyxerr << "LyX function 'space' needs an argument." << endl;
355 lyxerr << "Wrong argument for LyX function 'space'." << endl;
363 } catch (support::ExceptionMessage const & message) {
364 if (message.type_ == support::ErrorException) {
365 Alert::error(message.title_, message.details_);
366 LyX::cref().emergencyCleanup();
368 } else if (message.type_ == support::WarningException) {
369 Alert::warning(message.title_, message.details_);
379 Inset * readInset(Lexer & lex, Buffer const & buf)
382 if (lex.getString() != "\\begin_inset") {
383 lyxerr << "Buffer::readInset: Consistency check failed."
387 auto_ptr<Inset> inset;
389 TextClass const & tclass = buf.params().getTextClass();
392 string tmptok = lex.getString();
394 // test the different insets
396 //FIXME It would be better if we did not have this branch and could
397 //just do one massive switch for all insets. But at present, it's easier
398 //to do it this way, and we can't do the massive switch until the conversion
400 //Note that if we do want to do a single switch, we need to remove
401 //this "CommandInset" line---or replace it with a single "InsetType" line
402 //that would be used in all insets.
403 if (tmptok == "CommandInset") {
405 string const insetType = lex.getString();
406 lex.pushToken(insetType);
408 InsetCode const code = insetCode(insetType);
410 //FIXME If we do the one massive switch, we cannot do this here, since
411 //we do not know in advance that we're dealing with a command inset.
412 //Worst case, we could put it in each case below. Better, we could
413 //pass the lexer to the constructor and let the params be built there.
414 InsetCommandParams inscmd(code);
419 inset.reset(new InsetBibitem(inscmd));
422 inset.reset(new InsetBibtex(inscmd));
425 inset.reset(new InsetCitation(inscmd));
428 inset.reset(new InsetHyperlink(inscmd));
431 inset.reset(new InsetInclude(inscmd));
433 case INDEX_PRINT_CODE:
434 inset.reset(new InsetPrintIndex(inscmd));
437 inset.reset(new InsetLabel(inscmd));
440 inset.reset(new InsetNomencl(inscmd));
442 case NOMENCL_PRINT_CODE:
443 inset.reset(new InsetPrintNomencl(inscmd));
446 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
447 inset.reset(new InsetRef(inscmd, buf));
450 inset.reset(new InsetTOC(inscmd));
454 lyxerr << "unknown CommandInset '" << insetType
456 while (lex.isOK() && lex.getString() != "\\end_inset")
461 // FIXME This branch should be made to use inset codes as the preceding
462 // branch does. Unfortunately, that will take some doing. It requires
463 // converting the representation of the insets in LyX files so that they
464 // use the inset names listed in Inset.cpp. Then, as above, the inset names
465 // can be translated to inset codes using insetCode(). And the insets'
466 // write() routines should use insetName() rather than hardcoding it.
467 if (tmptok == "Quotes") {
468 inset.reset(new InsetQuotes);
469 } else if (tmptok == "External") {
470 inset.reset(new InsetExternal);
471 } else if (tmptok == "FormulaMacro") {
472 inset.reset(new MathMacroTemplate);
473 } else if (tmptok == "Formula") {
474 inset.reset(new InsetMathHull);
475 } else if (tmptok == "Graphics") {
476 inset.reset(new InsetGraphics);
477 } else if (tmptok == "Note") {
478 inset.reset(new InsetNote(buf.params(), tmptok));
479 } else if (tmptok == "Box") {
480 inset.reset(new InsetBox(buf.params(), tmptok));
481 } else if (tmptok == "Flex") {
483 string s = lex.getString();
484 InsetLayout const & il = tclass.insetlayout(from_utf8(s));
485 inset.reset(new InsetFlex(buf.params(), il));
486 } else if (tmptok == "Branch") {
487 inset.reset(new InsetBranch(buf.params(),
488 InsetBranchParams()));
489 } else if (tmptok == "Environment") {
491 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
492 } else if (tmptok == "ERT") {
493 inset.reset(new InsetERT(buf.params()));
494 } else if (tmptok == "listings") {
495 inset.reset(new InsetListings(buf.params()));
496 } else if (tmptok == "InsetSpace") {
497 inset.reset(new InsetSpace);
498 } else if (tmptok == "Tabular") {
499 inset.reset(new InsetTabular(buf));
500 } else if (tmptok == "Text") {
501 inset.reset(new InsetText(buf.params()));
502 } else if (tmptok == "VSpace") {
503 inset.reset(new InsetVSpace);
504 } else if (tmptok == "Foot") {
505 inset.reset(new InsetFoot(buf.params()));
506 } else if (tmptok == "Marginal") {
507 inset.reset(new InsetMarginal(buf.params()));
508 } else if (tmptok == "OptArg") {
509 inset.reset(new InsetOptArg(buf.params()));
510 } else if (tmptok == "Float") {
512 string tmptok = lex.getString();
513 inset.reset(new InsetFloat(buf.params(), tmptok));
514 } else if (tmptok == "Wrap") {
516 string tmptok = lex.getString();
517 inset.reset(new InsetWrap(buf.params(), tmptok));
519 } else if (tmptok == "Theorem") {
520 inset.reset(new InsetList);
522 } else if (tmptok == "Caption") {
523 inset.reset(new InsetCaption(buf.params()));
524 } else if (tmptok == "Index") {
525 inset.reset(new InsetIndex(buf.params()));
526 } else if (tmptok == "FloatList") {
527 inset.reset(new InsetFloatList);
528 } else if (tmptok == "Info") {
529 inset.reset(new InsetInfo(buf.params()));
531 lyxerr << "unknown Inset type '" << tmptok
533 while (lex.isOK() && lex.getString() != "\\end_inset")
538 inset->read(buf, lex);
541 return inset.release();