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")
175 return new InsetWrap(params, argument);
176 lyxerr << "Non-existent wrapfig type: " << argument << endl;
180 case LFUN_INDEX_INSERT: {
181 // Try and generate a valid index entry.
182 InsetCommandParams icp("index");
183 icp["name"] = cmd.argument().empty() ?
184 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
186 return new InsetIndex(icp);
189 case LFUN_NOMENCL_INSERT: {
190 InsetCommandParams icp("nomenclature");
191 icp["symbol"] = cmd.argument().empty() ?
192 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
194 return new InsetNomencl(icp);
197 case LFUN_TABULAR_INSERT: {
198 if (cmd.argument().empty())
200 std::istringstream ss(to_utf8(cmd.argument()));
207 return new InsetTabular(bv->buffer(), r, c);
210 case LFUN_CAPTION_INSERT: {
211 auto_ptr<InsetCaption> inset(new InsetCaption(params));
212 inset->setAutoBreakRows(true);
213 inset->setDrawFrame(true);
214 inset->setFrameColor(Color::captionframe);
215 return inset.release();
218 case LFUN_INDEX_PRINT:
219 return new InsetPrintIndex(InsetCommandParams("index_print"));
221 case LFUN_NOMENCL_PRINT:
222 return new InsetPrintNomencl(InsetCommandParams("nomencl_print"));
224 case LFUN_TOC_INSERT:
225 return new InsetTOC(InsetCommandParams("toc"));
227 case LFUN_ENVIRONMENT_INSERT:
228 return new InsetEnvironment(params, cmd.argument());
231 case LFUN_LIST_INSERT:
232 return new InsetList;
234 case LFUN_THEOREM_INSERT:
235 return new InsetTheorem;
238 case LFUN_INSET_INSERT: {
239 string const name = cmd.getArg(0);
241 if (name == "bibitem") {
242 InsetCommandParams icp(name);
243 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
245 return new InsetBibitem(icp);
247 } else if (name == "bibtex") {
248 InsetCommandParams icp(name);
249 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
251 return new InsetBibtex(icp);
253 } else if (name == "citation") {
254 InsetCommandParams icp("citation");
255 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
257 return new InsetCitation(icp);
259 } else if (name == "ert") {
260 InsetCollapsable::CollapseStatus st;
261 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
262 return new InsetERT(params, st);
264 } else if (name == "listings") {
265 InsetListingsParams par;
266 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
267 return new InsetListings(params, par);
269 } else if (name == "external") {
270 Buffer const & buffer = bv->buffer();
271 InsetExternalParams iep;
272 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
274 auto_ptr<InsetExternal> inset(new InsetExternal);
275 inset->setParams(iep, buffer);
276 return inset.release();
278 } else if (name == "graphics") {
279 Buffer const & buffer = bv->buffer();
280 InsetGraphicsParams igp;
281 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
283 auto_ptr<InsetGraphics> inset(new InsetGraphics);
284 inset->setParams(igp);
285 return inset.release();
287 } else if (name == "include") {
288 InsetCommandParams iip(name);
289 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
290 return new InsetInclude(iip);
292 } else if (name == "index") {
293 InsetCommandParams icp(name);
294 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
296 return new InsetIndex(icp);
298 } else if (name == "nomenclature") {
299 InsetCommandParams icp(name);
300 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
302 return new InsetNomencl(icp);
304 } else if (name == "label") {
305 InsetCommandParams icp(name);
306 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
308 return new InsetLabel(icp);
310 } else if (name == "ref") {
311 InsetCommandParams icp(name);
312 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
314 return new InsetRef(icp, bv->buffer());
316 } else if (name == "toc") {
317 InsetCommandParams icp("toc");
318 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
320 return new InsetTOC(icp);
322 } else if (name == "url") {
323 InsetCommandParams icp(name);
324 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
326 return new InsetUrl(icp);
328 } else if (name == "vspace") {
330 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
331 return new InsetVSpace(vspace);
335 case LFUN_SPACE_INSERT: {
336 string const name = to_utf8(cmd.argument());
337 if (name == "normal")
338 return new InsetSpace(InsetSpace::NORMAL);
339 else if (name == "protected")
340 return new InsetSpace(InsetSpace::PROTECTED);
341 else if (name == "thin")
342 return new InsetSpace(InsetSpace::THIN);
343 else if (name == "quad")
344 return new InsetSpace(InsetSpace::QUAD);
345 else if (name == "qquad")
346 return new InsetSpace(InsetSpace::QQUAD);
347 else if (name == "enspace")
348 return new InsetSpace(InsetSpace::ENSPACE);
349 else if (name == "enskip")
350 return new InsetSpace(InsetSpace::ENSKIP);
351 else if (name == "negthinspace")
352 return new InsetSpace(InsetSpace::NEGTHIN);
353 else if (name.empty())
354 lyxerr << "LyX function 'space' needs an argument." << endl;
356 lyxerr << "Wrong argument for LyX function 'space'." << endl;
364 } catch (support::ExceptionMessage const & message) {
365 if (message.type_ == support::ErrorException) {
366 Alert::error(message.title_, message.details_);
367 LyX::cref().emergencyCleanup();
369 } else if (message.type_ == support::WarningException) {
370 Alert::warning(message.title_, message.details_);
380 Inset * readInset(Lexer & lex, Buffer const & buf)
383 if (lex.getString() != "\\begin_inset") {
384 lyxerr << "Buffer::readInset: Consistency check failed."
388 auto_ptr<Inset> inset;
390 TextClass tclass = buf.params().getTextClass();
393 string tmptok = lex.getString();
395 // test the different insets
396 if (tmptok == "CommandInset") {
398 string const insetType = lex.getString();
399 lex.pushToken(insetType);
402 //Inset::Code const code = Inset::translate(insetType);
403 //if (code == Inset::NO_CODE) { choke as below; }
404 //InsetCommandParams inscmd();
405 InsetCommandParams inscmd(insetType);
408 if (insetType == "citation") {
409 inset.reset(new InsetCitation(inscmd));
410 } else if (insetType == "bibitem") {
411 inset.reset(new InsetBibitem(inscmd));
412 } else if (insetType == "bibtex") {
413 inset.reset(new InsetBibtex(inscmd));
414 } else if (insetType == "index") {
415 inset.reset(new InsetIndex(inscmd));
416 } else if (insetType == "nomenclature") {
417 inset.reset(new InsetNomencl(inscmd));
418 } else if (insetType == "include") {
419 inset.reset(new InsetInclude(inscmd));
420 } else if (insetType == "label") {
421 inset.reset(new InsetLabel(inscmd));
422 } else if (insetType == "url") {
423 inset.reset(new InsetUrl(inscmd));
424 } else if (insetType == "ref") {
425 if (!inscmd["name"].empty()
426 || !inscmd["reference"].empty()) {
427 inset.reset(new InsetRef(inscmd, buf));
429 } else if (insetType == "toc") {
430 inset.reset(new InsetTOC(inscmd));
431 } else if (insetType == "index_print") {
432 inset.reset(new InsetPrintIndex(inscmd));
433 } else if (insetType == "nomencl_print") {
434 inset.reset(new InsetPrintNomencl(inscmd));
436 lyxerr << "unknown CommandInset '" << insetType
438 while (lex.isOK() && lex.getString() != "\\end_inset")
443 if (tmptok == "Quotes") {
444 inset.reset(new InsetQuotes);
445 } else if (tmptok == "External") {
446 inset.reset(new InsetExternal);
447 } else if (tmptok == "FormulaMacro") {
448 inset.reset(new MathMacroTemplate);
449 } else if (tmptok == "Formula") {
450 inset.reset(new InsetMathHull);
451 } else if (tmptok == "Graphics") {
452 inset.reset(new InsetGraphics);
453 } else if (tmptok == "Note") {
454 inset.reset(new InsetNote(buf.params(), tmptok));
455 } else if (tmptok == "Box") {
456 inset.reset(new InsetBox(buf.params(), tmptok));
457 } else if (tmptok == "Flex") {
459 string s = lex.getString();
460 InsetLayout il = tclass.insetlayout(from_utf8(s));
461 inset.reset(new InsetFlex(buf.params(), il));
462 } else if (tmptok == "Branch") {
463 inset.reset(new InsetBranch(buf.params(),
464 InsetBranchParams()));
465 } else if (tmptok == "Include") {
466 InsetCommandParams p("include");
467 inset.reset(new InsetInclude(p));
468 } else if (tmptok == "Environment") {
470 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
471 } else if (tmptok == "ERT") {
472 inset.reset(new InsetERT(buf.params()));
473 } else if (tmptok == "listings") {
474 inset.reset(new InsetListings(buf.params()));
475 } else if (tmptok == "InsetSpace") {
476 inset.reset(new InsetSpace);
477 } else if (tmptok == "Tabular") {
478 inset.reset(new InsetTabular(buf));
479 } else if (tmptok == "Text") {
480 inset.reset(new InsetText(buf.params()));
481 } else if (tmptok == "VSpace") {
482 inset.reset(new InsetVSpace);
483 } else if (tmptok == "Foot") {
484 inset.reset(new InsetFoot(buf.params()));
485 } else if (tmptok == "Marginal") {
486 inset.reset(new InsetMarginal(buf.params()));
487 } else if (tmptok == "OptArg") {
488 inset.reset(new InsetOptArg(buf.params()));
489 } else if (tmptok == "Float") {
491 string tmptok = lex.getString();
492 inset.reset(new InsetFloat(buf.params(), tmptok));
493 } else if (tmptok == "Wrap") {
495 string tmptok = lex.getString();
496 inset.reset(new InsetWrap(buf.params(), tmptok));
498 } else if (tmptok == "List") {
499 inset.reset(new InsetList);
500 } else if (tmptok == "Theorem") {
501 inset.reset(new InsetList);
503 } else if (tmptok == "Caption") {
504 inset.reset(new InsetCaption(buf.params()));
505 } else if (tmptok == "FloatList") {
506 inset.reset(new InsetFloatList);
508 lyxerr << "unknown Inset type '" << tmptok
510 while (lex.isOK() && lex.getString() != "\\end_inset")
515 inset->read(buf, lex);
518 if (inset->lyxCode() == Inset::MATHMACRO_CODE) {
519 MathMacroTemplate const * tmpl =
520 static_cast<MathMacroTemplate*>(inset.get());
521 MacroTable::globalMacros().insert
522 (tmpl->name(), tmpl->asMacroData());
524 << BOOST_CURRENT_FUNCTION
525 << ": creating local macro " << to_utf8(tmpl->name())
530 return inset.release();