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"));
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("nomenclature");
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"));
216 case LFUN_NOMENCL_PRINT:
217 return new InsetPrintNomencl(InsetCommandParams("nomencl_print"));
219 case LFUN_TOC_INSERT:
220 return new InsetTOC(InsetCommandParams("toc"));
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(name);
242 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
243 return new InsetBibitem(icp);
247 InsetCommandParams icp(name);
248 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
249 return new InsetBibtex(icp);
253 InsetCommandParams icp("citation");
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(name);
290 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
291 return new InsetHyperlink(icp);
295 InsetCommandParams iip(name);
296 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
297 return new InsetInclude(iip);
301 return new InsetIndex(params);
304 InsetCommandParams icp(name);
305 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
306 return new InsetNomencl(icp);
310 InsetCommandParams icp(name);
311 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
312 return new InsetLabel(icp);
316 InsetCommandParams icp(name);
317 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
318 return new InsetRef(icp, bv->buffer());
322 InsetCommandParams icp("toc");
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."
338 } //end LFUN_INSET_INSERT
340 case LFUN_SPACE_INSERT: {
341 string const name = to_utf8(cmd.argument());
342 if (name == "normal")
343 return new InsetSpace(InsetSpace::NORMAL);
344 else if (name == "protected")
345 return new InsetSpace(InsetSpace::PROTECTED);
346 else if (name == "thin")
347 return new InsetSpace(InsetSpace::THIN);
348 else if (name == "quad")
349 return new InsetSpace(InsetSpace::QUAD);
350 else if (name == "qquad")
351 return new InsetSpace(InsetSpace::QQUAD);
352 else if (name == "enspace")
353 return new InsetSpace(InsetSpace::ENSPACE);
354 else if (name == "enskip")
355 return new InsetSpace(InsetSpace::ENSKIP);
356 else if (name == "negthinspace")
357 return new InsetSpace(InsetSpace::NEGTHIN);
358 else if (name.empty())
359 lyxerr << "LyX function 'space' needs an argument." << endl;
361 lyxerr << "Wrong argument for LyX function 'space'." << endl;
369 } catch (support::ExceptionMessage const & message) {
370 if (message.type_ == support::ErrorException) {
371 Alert::error(message.title_, message.details_);
372 LyX::cref().emergencyCleanup();
374 } else if (message.type_ == support::WarningException) {
375 Alert::warning(message.title_, message.details_);
385 Inset * readInset(Lexer & lex, Buffer const & buf)
388 if (lex.getString() != "\\begin_inset") {
389 lyxerr << "Buffer::readInset: Consistency check failed."
393 auto_ptr<Inset> inset;
395 TextClass tclass = buf.params().getTextClass();
398 string tmptok = lex.getString();
400 // test the different insets
402 //FIXME It would be better if we did not have this branch and could
403 //just do one massive switch for all insets. But at present, it's easier
404 //to do it this way, and we can't do the massive switch until the conversion
406 //Note that if we do want to do a single switch, we need to remove
407 //this "CommandInset" line---or replace it with a single "InsetType" line
408 //that would be used in all insets.
409 if (tmptok == "CommandInset") {
411 string const insetType = lex.getString();
412 lex.pushToken(insetType);
414 InsetCode const code = insetCode(insetType);
416 //FIXME If we do the one massive switch, we cannot do this here, since
417 //we do not know in advance that we're dealing with a command inset.
418 //Worst case, we could put it in each case below. Better, we could
419 //pass the lexer to the constructor and let the params be built there.
420 InsetCommandParams inscmd(insetType);
425 inset.reset(new InsetBibitem(inscmd));
428 inset.reset(new InsetBibtex(inscmd));
431 inset.reset(new InsetCitation(inscmd));
434 inset.reset(new InsetHyperlink(inscmd));
436 // FIXME Currently non-functional, since InsetInclude
437 // does not write itself as a CommandInset.
439 inset.reset(new InsetInclude(inscmd));
442 inset.reset(new InsetIndex(buf.params()));
444 case INDEX_PRINT_CODE:
445 inset.reset(new InsetPrintIndex(inscmd));
448 inset.reset(new InsetLabel(inscmd));
451 inset.reset(new InsetNomencl(inscmd));
453 case NOMENCL_PRINT_CODE:
454 inset.reset(new InsetPrintNomencl(inscmd));
457 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
458 inset.reset(new InsetRef(inscmd, buf));
461 inset.reset(new InsetTOC(inscmd));
465 lyxerr << "unknown CommandInset '" << insetType
467 while (lex.isOK() && lex.getString() != "\\end_inset")
472 // FIXME This branch should be made to use inset codes as the preceding
473 // branch does. Unfortunately, that will take some doing. It requires
474 // converting the representation of the insets in LyX files so that they
475 // use the inset names listed in Inset.cpp. Then, as above, the inset names
476 // can be translated to inset codes using insetCode(). And the insets'
477 // write() routines should use insetName() rather than hardcoding it.
478 if (tmptok == "Quotes") {
479 inset.reset(new InsetQuotes);
480 } else if (tmptok == "External") {
481 inset.reset(new InsetExternal);
482 } else if (tmptok == "FormulaMacro") {
483 inset.reset(new MathMacroTemplate);
484 } else if (tmptok == "Formula") {
485 inset.reset(new InsetMathHull);
486 } else if (tmptok == "Graphics") {
487 inset.reset(new InsetGraphics);
488 } else if (tmptok == "Note") {
489 inset.reset(new InsetNote(buf.params(), tmptok));
490 } else if (tmptok == "Box") {
491 inset.reset(new InsetBox(buf.params(), tmptok));
492 } else if (tmptok == "Flex") {
494 string s = lex.getString();
495 InsetLayout il = tclass.insetlayout(from_utf8(s));
496 inset.reset(new InsetFlex(buf.params(), il));
497 } else if (tmptok == "Branch") {
498 inset.reset(new InsetBranch(buf.params(),
499 InsetBranchParams()));
500 } else if (tmptok == "Include") {
502 InsetCommandParams p("include");
503 inset.reset(new InsetInclude(p));
504 } else if (tmptok == "Environment") {
506 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
507 } else if (tmptok == "ERT") {
508 inset.reset(new InsetERT(buf.params()));
509 } else if (tmptok == "listings") {
510 inset.reset(new InsetListings(buf.params()));
511 } else if (tmptok == "InsetSpace") {
512 inset.reset(new InsetSpace);
513 } else if (tmptok == "Tabular") {
514 inset.reset(new InsetTabular(buf));
515 } else if (tmptok == "Text") {
516 inset.reset(new InsetText(buf.params()));
517 } else if (tmptok == "VSpace") {
518 inset.reset(new InsetVSpace);
519 } else if (tmptok == "Foot") {
520 inset.reset(new InsetFoot(buf.params()));
521 } else if (tmptok == "Marginal") {
522 inset.reset(new InsetMarginal(buf.params()));
523 } else if (tmptok == "OptArg") {
524 inset.reset(new InsetOptArg(buf.params()));
525 } else if (tmptok == "Float") {
527 string tmptok = lex.getString();
528 inset.reset(new InsetFloat(buf.params(), tmptok));
529 } else if (tmptok == "Wrap") {
531 string tmptok = lex.getString();
532 inset.reset(new InsetWrap(buf.params(), tmptok));
534 } else if (tmptok == "Theorem") {
535 inset.reset(new InsetList);
537 } else if (tmptok == "Caption") {
538 inset.reset(new InsetCaption(buf.params()));
539 } else if (tmptok == "Index") {
540 inset.reset(new InsetIndex(buf.params()));
541 } else if (tmptok == "FloatList") {
542 inset.reset(new InsetFloatList);
543 } else if (tmptok == "Info") {
544 inset.reset(new InsetInfo(buf.params()));
546 lyxerr << "unknown Inset type '" << tmptok
548 while (lex.isOK() && lex.getString() != "\\end_inset")
553 inset->read(buf, lex);
556 if (inset->lyxCode() == MATHMACRO_CODE) {
557 MathMacroTemplate const * tmpl =
558 static_cast<MathMacroTemplate*>(inset.get());
559 MacroTable::globalMacros().insert
560 (tmpl->name(), tmpl->asMacroData());
562 << BOOST_CURRENT_FUNCTION
563 << ": creating local macro " << to_utf8(tmpl->name())
568 return inset.release();