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/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/InsetUrl.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"));
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("nomenclature");
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"));
215 case LFUN_NOMENCL_PRINT:
216 return new InsetPrintNomencl(InsetCommandParams("nomencl_print"));
218 case LFUN_TOC_INSERT:
219 return new InsetTOC(InsetCommandParams("toc"));
221 case LFUN_ENVIRONMENT_INSERT:
222 return new InsetEnvironment(params, cmd.argument());
225 case LFUN_LIST_INSERT:
226 return new InsetList;
228 case LFUN_THEOREM_INSERT:
229 return new InsetTheorem;
232 case LFUN_INSET_INSERT: {
233 string const name = cmd.getArg(0);
235 if (name == "bibitem") {
236 InsetCommandParams icp(name);
237 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
239 return new InsetBibitem(icp);
241 } else if (name == "bibtex") {
242 InsetCommandParams icp(name);
243 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
245 return new InsetBibtex(icp);
247 } else if (name == "citation") {
248 InsetCommandParams icp("citation");
249 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
251 return new InsetCitation(icp);
253 } else if (name == "ert") {
254 InsetCollapsable::CollapseStatus st;
255 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
256 return new InsetERT(params, st);
258 } else if (name == "listings") {
259 InsetListingsParams par;
260 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
261 return new InsetListings(params, par);
263 } else if (name == "external") {
264 Buffer const & buffer = bv->buffer();
265 InsetExternalParams iep;
266 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
268 auto_ptr<InsetExternal> inset(new InsetExternal);
269 inset->setParams(iep, buffer);
270 return inset.release();
272 } else if (name == "graphics") {
273 Buffer const & buffer = bv->buffer();
274 InsetGraphicsParams igp;
275 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
277 auto_ptr<InsetGraphics> inset(new InsetGraphics);
278 inset->setParams(igp);
279 return inset.release();
281 } else if (name == "include") {
282 InsetCommandParams iip(name);
283 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
284 return new InsetInclude(iip);
286 } else if (name == "index") {
287 return new InsetIndex(params);
289 } else if (name == "nomenclature") {
290 InsetCommandParams icp(name);
291 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
293 return new InsetNomencl(icp);
295 } else if (name == "label") {
296 InsetCommandParams icp(name);
297 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
299 return new InsetLabel(icp);
301 } else if (name == "ref") {
302 InsetCommandParams icp(name);
303 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
305 return new InsetRef(icp, bv->buffer());
307 } else if (name == "toc") {
308 InsetCommandParams icp("toc");
309 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
311 return new InsetTOC(icp);
313 } else if (name == "url") {
314 InsetCommandParams icp(name);
315 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
317 return new InsetUrl(icp);
319 } else if (name == "vspace") {
321 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
322 return new InsetVSpace(vspace);
326 case LFUN_SPACE_INSERT: {
327 string const name = to_utf8(cmd.argument());
328 if (name == "normal")
329 return new InsetSpace(InsetSpace::NORMAL);
330 else if (name == "protected")
331 return new InsetSpace(InsetSpace::PROTECTED);
332 else if (name == "thin")
333 return new InsetSpace(InsetSpace::THIN);
334 else if (name == "quad")
335 return new InsetSpace(InsetSpace::QUAD);
336 else if (name == "qquad")
337 return new InsetSpace(InsetSpace::QQUAD);
338 else if (name == "enspace")
339 return new InsetSpace(InsetSpace::ENSPACE);
340 else if (name == "enskip")
341 return new InsetSpace(InsetSpace::ENSKIP);
342 else if (name == "negthinspace")
343 return new InsetSpace(InsetSpace::NEGTHIN);
344 else if (name.empty())
345 lyxerr << "LyX function 'space' needs an argument." << endl;
347 lyxerr << "Wrong argument for LyX function 'space'." << endl;
355 } catch (support::ExceptionMessage const & message) {
356 if (message.type_ == support::ErrorException) {
357 Alert::error(message.title_, message.details_);
358 LyX::cref().emergencyCleanup();
360 } else if (message.type_ == support::WarningException) {
361 Alert::warning(message.title_, message.details_);
371 Inset * readInset(Lexer & lex, Buffer const & buf)
374 if (lex.getString() != "\\begin_inset") {
375 lyxerr << "Buffer::readInset: Consistency check failed."
379 auto_ptr<Inset> inset;
381 TextClass tclass = buf.params().getTextClass();
384 string tmptok = lex.getString();
386 // test the different insets
387 if (tmptok == "CommandInset") {
389 string const insetType = lex.getString();
390 lex.pushToken(insetType);
393 //Inset::Code const code = Inset::translate(insetType);
394 //if (code == Inset::NO_CODE) { choke as below; }
395 //InsetCommandParams inscmd();
396 InsetCommandParams inscmd(insetType);
399 if (insetType == "citation") {
400 inset.reset(new InsetCitation(inscmd));
401 } else if (insetType == "bibitem") {
402 inset.reset(new InsetBibitem(inscmd));
403 } else if (insetType == "bibtex") {
404 inset.reset(new InsetBibtex(inscmd));
405 } else if (insetType == "index") {
406 inset.reset(new InsetIndex(buf.params()));
407 } else if (insetType == "nomenclature") {
408 inset.reset(new InsetNomencl(inscmd));
409 } else if (insetType == "include") {
410 inset.reset(new InsetInclude(inscmd));
411 } else if (insetType == "label") {
412 inset.reset(new InsetLabel(inscmd));
413 } else if (insetType == "url") {
414 inset.reset(new InsetUrl(inscmd));
415 } else if (insetType == "ref") {
416 if (!inscmd["name"].empty()
417 || !inscmd["reference"].empty()) {
418 inset.reset(new InsetRef(inscmd, buf));
420 } else if (insetType == "toc") {
421 inset.reset(new InsetTOC(inscmd));
422 } else if (insetType == "index_print") {
423 inset.reset(new InsetPrintIndex(inscmd));
424 } else if (insetType == "nomencl_print") {
425 inset.reset(new InsetPrintNomencl(inscmd));
427 lyxerr << "unknown CommandInset '" << insetType
429 while (lex.isOK() && lex.getString() != "\\end_inset")
434 if (tmptok == "Quotes") {
435 inset.reset(new InsetQuotes);
436 } else if (tmptok == "External") {
437 inset.reset(new InsetExternal);
438 } else if (tmptok == "FormulaMacro") {
439 inset.reset(new MathMacroTemplate);
440 } else if (tmptok == "Formula") {
441 inset.reset(new InsetMathHull);
442 } else if (tmptok == "Graphics") {
443 inset.reset(new InsetGraphics);
444 } else if (tmptok == "Note") {
445 inset.reset(new InsetNote(buf.params(), tmptok));
446 } else if (tmptok == "Box") {
447 inset.reset(new InsetBox(buf.params(), tmptok));
448 } else if (tmptok == "Flex") {
450 string s = lex.getString();
451 InsetLayout il = tclass.insetlayout(from_utf8(s));
452 inset.reset(new InsetFlex(buf.params(), il));
453 } else if (tmptok == "Branch") {
454 inset.reset(new InsetBranch(buf.params(),
455 InsetBranchParams()));
456 } else if (tmptok == "Include") {
457 InsetCommandParams p("include");
458 inset.reset(new InsetInclude(p));
459 } else if (tmptok == "Environment") {
461 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
462 } else if (tmptok == "ERT") {
463 inset.reset(new InsetERT(buf.params()));
464 } else if (tmptok == "listings") {
465 inset.reset(new InsetListings(buf.params()));
466 } else if (tmptok == "InsetSpace") {
467 inset.reset(new InsetSpace);
468 } else if (tmptok == "Tabular") {
469 inset.reset(new InsetTabular(buf));
470 } else if (tmptok == "Text") {
471 inset.reset(new InsetText(buf.params()));
472 } else if (tmptok == "VSpace") {
473 inset.reset(new InsetVSpace);
474 } else if (tmptok == "Foot") {
475 inset.reset(new InsetFoot(buf.params()));
476 } else if (tmptok == "Marginal") {
477 inset.reset(new InsetMarginal(buf.params()));
478 } else if (tmptok == "OptArg") {
479 inset.reset(new InsetOptArg(buf.params()));
480 } else if (tmptok == "Float") {
482 string tmptok = lex.getString();
483 inset.reset(new InsetFloat(buf.params(), tmptok));
484 } else if (tmptok == "Wrap") {
486 string tmptok = lex.getString();
487 inset.reset(new InsetWrap(buf.params(), tmptok));
489 } else if (tmptok == "List") {
490 inset.reset(new InsetList);
491 } else if (tmptok == "Theorem") {
492 inset.reset(new InsetList);
494 } else if (tmptok == "Caption") {
495 inset.reset(new InsetCaption(buf.params()));
496 } else if (tmptok == "Index") {
497 inset.reset(new InsetIndex(buf.params()));
498 } else if (tmptok == "FloatList") {
499 inset.reset(new InsetFloatList);
501 lyxerr << "unknown Inset type '" << tmptok
503 while (lex.isOK() && lex.getString() != "\\end_inset")
508 inset->read(buf, lex);
511 if (inset->lyxCode() == Inset::MATHMACRO_CODE) {
512 MathMacroTemplate const * tmpl =
513 static_cast<MathMacroTemplate*>(inset.get());
514 MacroTable::globalMacros().insert
515 (tmpl->name(), tmpl->asMacroData());
517 << BOOST_CURRENT_FUNCTION
518 << ": creating local macro " << to_utf8(tmpl->name())
523 return inset.release();