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 "BufferView.h"
17 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
25 #include "insets/InsetBibitem.h"
26 #include "insets/InsetBibtex.h"
27 #include "insets/InsetCaption.h"
28 #include "insets/InsetCitation.h"
29 #include "insets/InsetFlex.h"
30 #include "insets/InsetEnvironment.h"
31 #include "insets/InsetERT.h"
32 #include "insets/InsetListings.h"
33 #include "insets/InsetExternal.h"
34 #include "insets/InsetFloat.h"
35 #include "insets/InsetFloatList.h"
36 #include "insets/InsetFoot.h"
37 #include "insets/InsetGraphics.h"
38 #include "insets/InsetHFill.h"
39 #include "insets/InsetInclude.h"
40 #include "insets/InsetIndex.h"
41 #include "insets/InsetInfo.h"
42 #include "insets/InsetNomencl.h"
43 #include "insets/InsetLabel.h"
44 #include "insets/InsetLine.h"
45 #include "insets/InsetMarginal.h"
46 #include "insets/InsetNote.h"
47 #include "insets/InsetBox.h"
48 #include "insets/InsetBranch.h"
49 #include "insets/InsetOptArg.h"
50 #include "insets/InsetPagebreak.h"
51 #include "insets/InsetRef.h"
52 #include "insets/InsetSpace.h"
53 #include "insets/InsetTabular.h"
54 #include "insets/InsetTOC.h"
55 #include "insets/InsetHyperlink.h"
56 #include "insets/InsetVSpace.h"
57 #include "insets/InsetWrap.h"
59 #include "mathed/MathMacroTemplate.h"
60 #include "mathed/InsetMathHull.h"
62 #include "frontends/alert.h"
64 #include "support/lstrings.h"
65 #include "support/ExceptionMessage.h"
67 #include <boost/assert.hpp>
68 #include <boost/current_function.hpp>
79 namespace Alert = frontend::Alert;
81 using support::compare_ascii_no_case;
84 Inset * createInset(BufferView * bv, FuncRequest const & cmd)
86 BufferParams const & params = bv->buffer().params();
91 case LFUN_HFILL_INSERT:
92 return new InsetHFill;
94 case LFUN_LINE_INSERT:
97 case LFUN_PAGEBREAK_INSERT:
98 return new InsetPagebreak;
100 case LFUN_CLEARPAGE_INSERT:
101 return new InsetClearPage;
103 case LFUN_CLEARDOUBLEPAGE_INSERT:
104 return new InsetClearDoublePage;
106 case LFUN_FLEX_INSERT: {
107 string s = cmd.getArg(0);
108 TextClass tclass = params.getTextClass();
109 InsetLayout il = tclass.insetlayout(from_utf8(s));
110 return new InsetFlex(params, il);
113 case LFUN_NOTE_INSERT: {
114 string arg = cmd.getArg(0);
117 return new InsetNote(params, arg);
120 case LFUN_BOX_INSERT: {
121 string arg = cmd.getArg(0);
124 return new InsetBox(params, arg);
127 case LFUN_BRANCH_INSERT: {
128 docstring arg = cmd.argument();
130 arg = from_ascii("none");
131 return new InsetBranch(params, InsetBranchParams(arg));
134 case LFUN_ERT_INSERT:
135 return new InsetERT(params);
137 case LFUN_LISTING_INSERT:
138 return new InsetListings(params);
140 case LFUN_FOOTNOTE_INSERT:
141 return new InsetFoot(params);
143 case LFUN_MARGINALNOTE_INSERT:
144 return new InsetMarginal(params);
146 case LFUN_OPTIONAL_INSERT:
147 return new InsetOptArg(params);
149 case LFUN_BIBITEM_INSERT:
150 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
152 case LFUN_FLOAT_INSERT: {
153 // check if the float type exists
154 string const argument = to_utf8(cmd.argument());
155 if (params.getTextClass().floats().typeExist(argument))
156 return new InsetFloat(params, argument);
157 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().empty() ?
187 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
189 return new InsetNomencl(icp);
192 case LFUN_TABULAR_INSERT: {
193 if (cmd.argument().empty())
195 std::istringstream ss(to_utf8(cmd.argument()));
202 return new InsetTabular(bv->buffer(), r, c);
205 case LFUN_CAPTION_INSERT: {
206 auto_ptr<InsetCaption> inset(new InsetCaption(params));
207 inset->setAutoBreakRows(true);
208 inset->setDrawFrame(true);
209 inset->setFrameColor(Color::captionframe);
210 return inset.release();
213 case LFUN_INDEX_PRINT:
214 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
216 case LFUN_NOMENCL_PRINT:
217 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
219 case LFUN_TOC_INSERT:
220 return new InsetTOC(InsetCommandParams(TOC_CODE));
222 case LFUN_ENVIRONMENT_INSERT:
223 return new InsetEnvironment(params, cmd.argument());
225 case LFUN_INFO_INSERT:
226 return new InsetInfo(params, to_utf8(cmd.argument()));
228 case LFUN_THEOREM_INSERT:
229 return new InsetTheorem;
232 case LFUN_INSET_INSERT: {
233 string const name = cmd.getArg(0);
234 InsetCode code = insetCode(name);
237 lyxerr << "No such inset '" << name << "'.";
241 InsetCommandParams icp(code);
242 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
243 return new InsetBibitem(icp);
247 InsetCommandParams icp(code);
248 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
249 return new InsetBibtex(icp);
253 InsetCommandParams icp(code);
254 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
255 return new InsetCitation(icp);
259 InsetCollapsable::CollapseStatus st;
260 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
261 return new InsetERT(params, st);
264 case LISTINGS_CODE: {
265 InsetListingsParams par;
266 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
267 return new InsetListings(params, par);
270 case EXTERNAL_CODE: {
271 Buffer const & buffer = bv->buffer();
272 InsetExternalParams iep;
273 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, iep);
274 auto_ptr<InsetExternal> inset(new InsetExternal);
275 inset->setParams(iep, buffer);
276 return inset.release();
279 case GRAPHICS_CODE: {
280 Buffer const & buffer = bv->buffer();
281 InsetGraphicsParams igp;
282 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, igp);
283 auto_ptr<InsetGraphics> inset(new InsetGraphics);
284 inset->setParams(igp);
285 return inset.release();
288 case HYPERLINK_CODE: {
289 InsetCommandParams icp(code);
290 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
291 return new InsetHyperlink(icp);
295 InsetCommandParams iip(code);
296 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
297 return new InsetInclude(iip);
301 return new InsetIndex(params);
304 InsetCommandParams icp(code);
305 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
306 return new InsetNomencl(icp);
310 InsetCommandParams icp(code);
311 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
312 return new InsetLabel(icp);
316 InsetCommandParams icp(code);
317 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
318 return new InsetRef(icp, bv->buffer());
322 InsetCommandParams icp(code);
323 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
324 return new InsetTOC(icp);
329 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
330 return new InsetVSpace(vspace);
334 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
339 } //end LFUN_INSET_INSERT
341 case LFUN_SPACE_INSERT: {
342 string const name = to_utf8(cmd.argument());
343 if (name == "normal")
344 return new InsetSpace(InsetSpace::NORMAL);
345 else if (name == "protected")
346 return new InsetSpace(InsetSpace::PROTECTED);
347 else if (name == "thin")
348 return new InsetSpace(InsetSpace::THIN);
349 else if (name == "quad")
350 return new InsetSpace(InsetSpace::QUAD);
351 else if (name == "qquad")
352 return new InsetSpace(InsetSpace::QQUAD);
353 else if (name == "enspace")
354 return new InsetSpace(InsetSpace::ENSPACE);
355 else if (name == "enskip")
356 return new InsetSpace(InsetSpace::ENSKIP);
357 else if (name == "negthinspace")
358 return new InsetSpace(InsetSpace::NEGTHIN);
359 else if (name.empty())
360 lyxerr << "LyX function 'space' needs an argument." << endl;
362 lyxerr << "Wrong argument for LyX function 'space'." << endl;
370 } catch (support::ExceptionMessage const & message) {
371 if (message.type_ == support::ErrorException) {
372 Alert::error(message.title_, message.details_);
373 LyX::cref().emergencyCleanup();
375 } else if (message.type_ == support::WarningException) {
376 Alert::warning(message.title_, message.details_);
386 Inset * readInset(Lexer & lex, Buffer const & buf)
389 if (lex.getString() != "\\begin_inset") {
390 lyxerr << "Buffer::readInset: Consistency check failed."
394 auto_ptr<Inset> inset;
396 TextClass tclass = buf.params().getTextClass();
399 string tmptok = lex.getString();
401 // test the different insets
403 //FIXME It would be better if we did not have this branch and could
404 //just do one massive switch for all insets. But at present, it's easier
405 //to do it this way, and we can't do the massive switch until the conversion
407 //Note that if we do want to do a single switch, we need to remove
408 //this "CommandInset" line---or replace it with a single "InsetType" line
409 //that would be used in all insets.
410 if (tmptok == "CommandInset") {
412 string const insetType = lex.getString();
413 lex.pushToken(insetType);
415 InsetCode const code = insetCode(insetType);
417 //FIXME If we do the one massive switch, we cannot do this here, since
418 //we do not know in advance that we're dealing with a command inset.
419 //Worst case, we could put it in each case below. Better, we could
420 //pass the lexer to the constructor and let the params be built there.
421 InsetCommandParams inscmd(code);
426 inset.reset(new InsetBibitem(inscmd));
429 inset.reset(new InsetBibtex(inscmd));
432 inset.reset(new InsetCitation(inscmd));
435 inset.reset(new InsetHyperlink(inscmd));
437 // FIXME Currently non-functional, since InsetInclude
438 // does not write itself as a CommandInset.
440 inset.reset(new InsetInclude(inscmd));
443 inset.reset(new InsetIndex(buf.params()));
445 case INDEX_PRINT_CODE:
446 inset.reset(new InsetPrintIndex(inscmd));
449 inset.reset(new InsetLabel(inscmd));
452 inset.reset(new InsetNomencl(inscmd));
454 case NOMENCL_PRINT_CODE:
455 inset.reset(new InsetPrintNomencl(inscmd));
458 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
459 inset.reset(new InsetRef(inscmd, buf));
462 inset.reset(new InsetTOC(inscmd));
466 lyxerr << "unknown CommandInset '" << insetType
468 while (lex.isOK() && lex.getString() != "\\end_inset")
473 // FIXME This branch should be made to use inset codes as the preceding
474 // branch does. Unfortunately, that will take some doing. It requires
475 // converting the representation of the insets in LyX files so that they
476 // use the inset names listed in Inset.cpp. Then, as above, the inset names
477 // can be translated to inset codes using insetCode(). And the insets'
478 // write() routines should use insetName() rather than hardcoding it.
479 if (tmptok == "Quotes") {
480 inset.reset(new InsetQuotes);
481 } else if (tmptok == "External") {
482 inset.reset(new InsetExternal);
483 } else if (tmptok == "FormulaMacro") {
484 inset.reset(new MathMacroTemplate);
485 } else if (tmptok == "Formula") {
486 inset.reset(new InsetMathHull);
487 } else if (tmptok == "Graphics") {
488 inset.reset(new InsetGraphics);
489 } else if (tmptok == "Note") {
490 inset.reset(new InsetNote(buf.params(), tmptok));
491 } else if (tmptok == "Box") {
492 inset.reset(new InsetBox(buf.params(), tmptok));
493 } else if (tmptok == "Flex") {
495 string s = lex.getString();
496 InsetLayout il = tclass.insetlayout(from_utf8(s));
497 inset.reset(new InsetFlex(buf.params(), il));
498 } else if (tmptok == "Branch") {
499 inset.reset(new InsetBranch(buf.params(),
500 InsetBranchParams()));
501 } else if (tmptok == "Include") {
503 InsetCommandParams p(INCLUDE_CODE);
504 inset.reset(new InsetInclude(p));
505 } else if (tmptok == "Environment") {
507 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
508 } else if (tmptok == "ERT") {
509 inset.reset(new InsetERT(buf.params()));
510 } else if (tmptok == "listings") {
511 inset.reset(new InsetListings(buf.params()));
512 } else if (tmptok == "InsetSpace") {
513 inset.reset(new InsetSpace);
514 } else if (tmptok == "Tabular") {
515 inset.reset(new InsetTabular(buf));
516 } else if (tmptok == "Text") {
517 inset.reset(new InsetText(buf.params()));
518 } else if (tmptok == "VSpace") {
519 inset.reset(new InsetVSpace);
520 } else if (tmptok == "Foot") {
521 inset.reset(new InsetFoot(buf.params()));
522 } else if (tmptok == "Marginal") {
523 inset.reset(new InsetMarginal(buf.params()));
524 } else if (tmptok == "OptArg") {
525 inset.reset(new InsetOptArg(buf.params()));
526 } else if (tmptok == "Float") {
528 string tmptok = lex.getString();
529 inset.reset(new InsetFloat(buf.params(), tmptok));
530 } else if (tmptok == "Wrap") {
532 string tmptok = lex.getString();
533 inset.reset(new InsetWrap(buf.params(), tmptok));
535 } else if (tmptok == "Theorem") {
536 inset.reset(new InsetList);
538 } else if (tmptok == "Caption") {
539 inset.reset(new InsetCaption(buf.params()));
540 } else if (tmptok == "Index") {
541 inset.reset(new InsetIndex(buf.params()));
542 } else if (tmptok == "FloatList") {
543 inset.reset(new InsetFloatList);
544 } else if (tmptok == "Info") {
545 inset.reset(new InsetInfo(buf.params()));
547 lyxerr << "unknown Inset type '" << tmptok
549 while (lex.isOK() && lex.getString() != "\\end_inset")
554 inset->read(buf, lex);
557 if (inset->lyxCode() == MATHMACRO_CODE) {
558 MathMacroTemplate const * tmpl =
559 static_cast<MathMacroTemplate*>(inset.get());
560 MacroTable::globalMacros().insert
561 (tmpl->name(), tmpl->asMacroData());
563 << BOOST_CURRENT_FUNCTION
564 << ": creating local macro " << to_utf8(tmpl->name())
569 return inset.release();