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"
24 #include "insets/InsetBibitem.h"
25 #include "insets/InsetBibtex.h"
26 #include "insets/InsetCaption.h"
27 #include "insets/InsetCitation.h"
28 #include "insets/InsetFlex.h"
29 #include "insets/InsetEnvironment.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetListings.h"
32 #include "insets/InsetExternal.h"
33 #include "insets/InsetFloat.h"
34 #include "insets/InsetFloatList.h"
35 #include "insets/InsetFoot.h"
36 #include "insets/InsetGraphics.h"
37 #include "insets/InsetHFill.h"
38 #include "insets/InsetInclude.h"
39 #include "insets/InsetIndex.h"
40 #include "insets/InsetInfo.h"
41 #include "insets/InsetNomencl.h"
42 #include "insets/InsetLabel.h"
43 #include "insets/InsetLine.h"
44 #include "insets/InsetMarginal.h"
45 #include "insets/InsetNote.h"
46 #include "insets/InsetBox.h"
47 #include "insets/InsetBranch.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetPagebreak.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.h"
54 #include "insets/InsetHyperlink.h"
55 #include "insets/InsetVSpace.h"
56 #include "insets/InsetWrap.h"
58 #include "mathed/MathMacroTemplate.h"
59 #include "mathed/InsetMathHull.h"
61 #include "frontends/alert.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
66 #include <boost/assert.hpp>
67 #include <boost/current_function.hpp>
78 namespace Alert = frontend::Alert;
80 using support::compare_ascii_no_case;
83 Inset * createInset(BufferView * bv, FuncRequest const & cmd)
85 BufferParams const & params = bv->buffer().params();
90 case LFUN_HFILL_INSERT:
91 return new InsetHFill;
93 case LFUN_LINE_INSERT:
96 case LFUN_PAGEBREAK_INSERT:
97 return new InsetPagebreak;
99 case LFUN_CLEARPAGE_INSERT:
100 return new InsetClearPage;
102 case LFUN_CLEARDOUBLEPAGE_INSERT:
103 return new InsetClearDoublePage;
105 case LFUN_FLEX_INSERT: {
106 string s = cmd.getArg(0);
107 TextClass tclass = params.getTextClass();
108 InsetLayout il = tclass.insetlayout(from_utf8(s));
109 return new InsetFlex(params, il);
112 case LFUN_NOTE_INSERT: {
113 string arg = cmd.getArg(0);
116 return new InsetNote(params, arg);
119 case LFUN_BOX_INSERT: {
120 string arg = cmd.getArg(0);
123 return new InsetBox(params, arg);
126 case LFUN_BRANCH_INSERT: {
127 docstring arg = cmd.argument();
129 arg = from_ascii("none");
130 return new InsetBranch(params, InsetBranchParams(arg));
133 case LFUN_ERT_INSERT:
134 return new InsetERT(params);
136 case LFUN_LISTING_INSERT:
137 return new InsetListings(params);
139 case LFUN_FOOTNOTE_INSERT:
140 return new InsetFoot(params);
142 case LFUN_MARGINALNOTE_INSERT:
143 return new InsetMarginal(params);
145 case LFUN_OPTIONAL_INSERT:
146 return new InsetOptArg(params);
148 case LFUN_BIBITEM_INSERT:
149 return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
151 case LFUN_FLOAT_INSERT: {
152 // check if the float type exists
153 string const argument = to_utf8(cmd.argument());
154 if (params.getTextClass().floats().typeExist(argument))
155 return new InsetFloat(params, argument);
156 lyxerr << "Non-existent float type: " << argument << endl;
160 case LFUN_FLOAT_WIDE_INSERT: {
161 // check if the float type exists
162 string const argument = to_utf8(cmd.argument());
163 if (params.getTextClass().floats().typeExist(argument)) {
164 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
165 p->wide(true, params);
168 lyxerr << "Non-existent float type: " << argument << endl;
172 case LFUN_WRAP_INSERT: {
173 string const argument = to_utf8(cmd.argument());
174 if (argument == "figure" || argument == "table")
175 return new InsetWrap(params, argument);
176 lyxerr << "Non-existent wrapfig type: " << argument << endl;
180 case LFUN_INDEX_INSERT:
181 return new InsetIndex(params);
183 case LFUN_NOMENCL_INSERT: {
184 InsetCommandParams icp(NOMENCL_CODE);
185 icp["symbol"] = cmd.argument().empty() ?
186 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
188 return new InsetNomencl(icp);
191 case LFUN_TABULAR_INSERT: {
192 if (cmd.argument().empty())
194 std::istringstream ss(to_utf8(cmd.argument()));
201 return new InsetTabular(bv->buffer(), r, c);
204 case LFUN_CAPTION_INSERT: {
205 auto_ptr<InsetCaption> inset(new InsetCaption(params));
206 inset->setAutoBreakRows(true);
207 inset->setDrawFrame(true);
208 inset->setFrameColor(Color_captionframe);
209 return inset.release();
212 case LFUN_INDEX_PRINT:
213 return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
215 case LFUN_NOMENCL_PRINT:
216 return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
218 case LFUN_TOC_INSERT:
219 return new InsetTOC(InsetCommandParams(TOC_CODE));
221 case LFUN_ENVIRONMENT_INSERT:
222 return new InsetEnvironment(params, cmd.argument());
224 case LFUN_INFO_INSERT:
225 return new InsetInfo(params, to_utf8(cmd.argument()));
227 case LFUN_THEOREM_INSERT:
228 return new InsetTheorem;
231 case LFUN_INSET_INSERT: {
232 string const name = cmd.getArg(0);
233 InsetCode code = insetCode(name);
236 lyxerr << "No such inset '" << name << "'.";
240 InsetCommandParams icp(code);
241 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
242 return new InsetBibitem(icp);
246 InsetCommandParams icp(code);
247 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
248 return new InsetBibtex(icp);
252 InsetCommandParams icp(code);
253 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
254 return new InsetCitation(icp);
258 InsetCollapsable::CollapseStatus st;
259 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
260 return new InsetERT(params, st);
263 case LISTINGS_CODE: {
264 InsetListingsParams par;
265 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
266 return new InsetListings(params, par);
269 case EXTERNAL_CODE: {
270 Buffer const & buffer = bv->buffer();
271 InsetExternalParams iep;
272 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, iep);
273 auto_ptr<InsetExternal> inset(new InsetExternal);
274 inset->setParams(iep, buffer);
275 return inset.release();
278 case GRAPHICS_CODE: {
279 Buffer const & buffer = bv->buffer();
280 InsetGraphicsParams igp;
281 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, igp);
282 auto_ptr<InsetGraphics> inset(new InsetGraphics);
283 inset->setParams(igp);
284 return inset.release();
287 case HYPERLINK_CODE: {
288 InsetCommandParams icp(code);
289 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
290 return new InsetHyperlink(icp);
294 InsetCommandParams icp(code);
295 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
296 return new InsetInclude(icp);
300 return new InsetIndex(params);
303 InsetCommandParams icp(code);
304 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
305 return new InsetNomencl(icp);
309 InsetCommandParams icp(code);
310 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
311 return new InsetLabel(icp);
315 InsetCommandParams icp(code);
316 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
317 return new InsetRef(icp, bv->buffer());
321 InsetCommandParams icp(code);
322 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
323 return new InsetTOC(icp);
328 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
329 return new InsetVSpace(vspace);
333 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(code);
425 inset.reset(new InsetBibitem(inscmd));
428 inset.reset(new InsetBibtex(inscmd));
431 inset.reset(new InsetCitation(inscmd));
434 inset.reset(new InsetHyperlink(inscmd));
437 inset.reset(new InsetInclude(inscmd));
439 case INDEX_PRINT_CODE:
440 inset.reset(new InsetPrintIndex(inscmd));
443 inset.reset(new InsetLabel(inscmd));
446 inset.reset(new InsetNomencl(inscmd));
448 case NOMENCL_PRINT_CODE:
449 inset.reset(new InsetPrintNomencl(inscmd));
452 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
453 inset.reset(new InsetRef(inscmd, buf));
456 inset.reset(new InsetTOC(inscmd));
460 lyxerr << "unknown CommandInset '" << insetType
462 while (lex.isOK() && lex.getString() != "\\end_inset")
467 // FIXME This branch should be made to use inset codes as the preceding
468 // branch does. Unfortunately, that will take some doing. It requires
469 // converting the representation of the insets in LyX files so that they
470 // use the inset names listed in Inset.cpp. Then, as above, the inset names
471 // can be translated to inset codes using insetCode(). And the insets'
472 // write() routines should use insetName() rather than hardcoding it.
473 if (tmptok == "Quotes") {
474 inset.reset(new InsetQuotes);
475 } else if (tmptok == "External") {
476 inset.reset(new InsetExternal);
477 } else if (tmptok == "FormulaMacro") {
478 inset.reset(new MathMacroTemplate);
479 } else if (tmptok == "Formula") {
480 inset.reset(new InsetMathHull);
481 } else if (tmptok == "Graphics") {
482 inset.reset(new InsetGraphics);
483 } else if (tmptok == "Note") {
484 inset.reset(new InsetNote(buf.params(), tmptok));
485 } else if (tmptok == "Box") {
486 inset.reset(new InsetBox(buf.params(), tmptok));
487 } else if (tmptok == "Flex") {
489 string s = lex.getString();
490 InsetLayout il = tclass.insetlayout(from_utf8(s));
491 inset.reset(new InsetFlex(buf.params(), il));
492 } else if (tmptok == "Branch") {
493 inset.reset(new InsetBranch(buf.params(),
494 InsetBranchParams()));
495 } else if (tmptok == "Environment") {
497 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
498 } else if (tmptok == "ERT") {
499 inset.reset(new InsetERT(buf.params()));
500 } else if (tmptok == "listings") {
501 inset.reset(new InsetListings(buf.params()));
502 } else if (tmptok == "InsetSpace") {
503 inset.reset(new InsetSpace);
504 } else if (tmptok == "Tabular") {
505 inset.reset(new InsetTabular(buf));
506 } else if (tmptok == "Text") {
507 inset.reset(new InsetText(buf.params()));
508 } else if (tmptok == "VSpace") {
509 inset.reset(new InsetVSpace);
510 } else if (tmptok == "Foot") {
511 inset.reset(new InsetFoot(buf.params()));
512 } else if (tmptok == "Marginal") {
513 inset.reset(new InsetMarginal(buf.params()));
514 } else if (tmptok == "OptArg") {
515 inset.reset(new InsetOptArg(buf.params()));
516 } else if (tmptok == "Float") {
518 string tmptok = lex.getString();
519 inset.reset(new InsetFloat(buf.params(), tmptok));
520 } else if (tmptok == "Wrap") {
522 string tmptok = lex.getString();
523 inset.reset(new InsetWrap(buf.params(), tmptok));
525 } else if (tmptok == "Theorem") {
526 inset.reset(new InsetList);
528 } else if (tmptok == "Caption") {
529 inset.reset(new InsetCaption(buf.params()));
530 } else if (tmptok == "Index") {
531 inset.reset(new InsetIndex(buf.params()));
532 } else if (tmptok == "FloatList") {
533 inset.reset(new InsetFloatList);
534 } else if (tmptok == "Info") {
535 inset.reset(new InsetInfo(buf.params()));
537 lyxerr << "unknown Inset type '" << tmptok
539 while (lex.isOK() && lex.getString() != "\\end_inset")
544 inset->read(buf, lex);
547 return inset.release();