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_LIST_INSERT:
229 return new InsetList;
231 case LFUN_THEOREM_INSERT:
232 return new InsetTheorem;
235 case LFUN_INSET_INSERT: {
236 string const name = cmd.getArg(0);
238 if (name == "bibitem") {
239 InsetCommandParams icp(name);
240 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
242 return new InsetBibitem(icp);
244 } else if (name == "bibtex") {
245 InsetCommandParams icp(name);
246 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
248 return new InsetBibtex(icp);
250 } else if (name == "citation") {
251 InsetCommandParams icp("citation");
252 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
254 return new InsetCitation(icp);
256 } else if (name == "ert") {
257 InsetCollapsable::CollapseStatus st;
258 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
259 return new InsetERT(params, st);
261 } else if (name == "listings") {
262 InsetListingsParams par;
263 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
264 return new InsetListings(params, par);
266 } else if (name == "external") {
267 Buffer const & buffer = bv->buffer();
268 InsetExternalParams iep;
269 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
271 auto_ptr<InsetExternal> inset(new InsetExternal);
272 inset->setParams(iep, buffer);
273 return inset.release();
275 } else if (name == "graphics") {
276 Buffer const & buffer = bv->buffer();
277 InsetGraphicsParams igp;
278 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
280 auto_ptr<InsetGraphics> inset(new InsetGraphics);
281 inset->setParams(igp);
282 return inset.release();
284 } else if (name == "href") {
285 InsetCommandParams icp(name);
286 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
288 return new InsetHyperlink(icp);
290 } else if (name == "include") {
291 InsetCommandParams iip(name);
292 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
293 return new InsetInclude(iip);
295 } else if (name == "index") {
296 return new InsetIndex(params);
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 == "vspace") {
324 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
325 return new InsetVSpace(vspace);
329 case LFUN_SPACE_INSERT: {
330 string const name = to_utf8(cmd.argument());
331 if (name == "normal")
332 return new InsetSpace(InsetSpace::NORMAL);
333 else if (name == "protected")
334 return new InsetSpace(InsetSpace::PROTECTED);
335 else if (name == "thin")
336 return new InsetSpace(InsetSpace::THIN);
337 else if (name == "quad")
338 return new InsetSpace(InsetSpace::QUAD);
339 else if (name == "qquad")
340 return new InsetSpace(InsetSpace::QQUAD);
341 else if (name == "enspace")
342 return new InsetSpace(InsetSpace::ENSPACE);
343 else if (name == "enskip")
344 return new InsetSpace(InsetSpace::ENSKIP);
345 else if (name == "negthinspace")
346 return new InsetSpace(InsetSpace::NEGTHIN);
347 else if (name.empty())
348 lyxerr << "LyX function 'space' needs an argument." << endl;
350 lyxerr << "Wrong argument for LyX function 'space'." << endl;
358 } catch (support::ExceptionMessage const & message) {
359 if (message.type_ == support::ErrorException) {
360 Alert::error(message.title_, message.details_);
361 LyX::cref().emergencyCleanup();
363 } else if (message.type_ == support::WarningException) {
364 Alert::warning(message.title_, message.details_);
374 Inset * readInset(Lexer & lex, Buffer const & buf)
377 if (lex.getString() != "\\begin_inset") {
378 lyxerr << "Buffer::readInset: Consistency check failed."
382 auto_ptr<Inset> inset;
384 TextClass tclass = buf.params().getTextClass();
387 string tmptok = lex.getString();
389 // test the different insets
390 if (tmptok == "CommandInset") {
392 string const insetType = lex.getString();
393 lex.pushToken(insetType);
396 //InsetCode const code = Inset::translate(insetType);
397 //if (code == NO_CODE) { choke as below; }
398 //InsetCommandParams inscmd();
399 InsetCommandParams inscmd(insetType);
402 if (insetType == "citation") {
403 inset.reset(new InsetCitation(inscmd));
404 } else if (insetType == "bibitem") {
405 inset.reset(new InsetBibitem(inscmd));
406 } else if (insetType == "bibtex") {
407 inset.reset(new InsetBibtex(inscmd));
408 } else if (insetType == "index") {
409 inset.reset(new InsetIndex(buf.params()));
410 } else if (insetType == "href") {
411 inset.reset(new InsetHyperlink(inscmd));
412 } else if (insetType == "include") {
413 inset.reset(new InsetInclude(inscmd));
414 } else if (insetType == "label") {
415 inset.reset(new InsetLabel(inscmd));
416 } else if (insetType == "nomenclature") {
417 inset.reset(new InsetNomencl(inscmd));
418 } else if (insetType == "ref") {
419 if (!inscmd["name"].empty()
420 || !inscmd["reference"].empty()) {
421 inset.reset(new InsetRef(inscmd, buf));
423 } else if (insetType == "toc") {
424 inset.reset(new InsetTOC(inscmd));
425 } else if (insetType == "index_print") {
426 inset.reset(new InsetPrintIndex(inscmd));
427 } else if (insetType == "nomencl_print") {
428 inset.reset(new InsetPrintNomencl(inscmd));
430 lyxerr << "unknown CommandInset '" << insetType
432 while (lex.isOK() && lex.getString() != "\\end_inset")
437 if (tmptok == "Quotes") {
438 inset.reset(new InsetQuotes);
439 } else if (tmptok == "External") {
440 inset.reset(new InsetExternal);
441 } else if (tmptok == "FormulaMacro") {
442 inset.reset(new MathMacroTemplate);
443 } else if (tmptok == "Formula") {
444 inset.reset(new InsetMathHull);
445 } else if (tmptok == "Graphics") {
446 inset.reset(new InsetGraphics);
447 } else if (tmptok == "Note") {
448 inset.reset(new InsetNote(buf.params(), tmptok));
449 } else if (tmptok == "Box") {
450 inset.reset(new InsetBox(buf.params(), tmptok));
451 } else if (tmptok == "Flex") {
453 string s = lex.getString();
454 InsetLayout il = tclass.insetlayout(from_utf8(s));
455 inset.reset(new InsetFlex(buf.params(), il));
456 } else if (tmptok == "Branch") {
457 inset.reset(new InsetBranch(buf.params(),
458 InsetBranchParams()));
459 } else if (tmptok == "Include") {
460 InsetCommandParams p("include");
461 inset.reset(new InsetInclude(p));
462 } else if (tmptok == "Environment") {
464 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
465 } else if (tmptok == "ERT") {
466 inset.reset(new InsetERT(buf.params()));
467 } else if (tmptok == "listings") {
468 inset.reset(new InsetListings(buf.params()));
469 } else if (tmptok == "InsetSpace") {
470 inset.reset(new InsetSpace);
471 } else if (tmptok == "Tabular") {
472 inset.reset(new InsetTabular(buf));
473 } else if (tmptok == "Text") {
474 inset.reset(new InsetText(buf.params()));
475 } else if (tmptok == "VSpace") {
476 inset.reset(new InsetVSpace);
477 } else if (tmptok == "Foot") {
478 inset.reset(new InsetFoot(buf.params()));
479 } else if (tmptok == "Marginal") {
480 inset.reset(new InsetMarginal(buf.params()));
481 } else if (tmptok == "OptArg") {
482 inset.reset(new InsetOptArg(buf.params()));
483 } else if (tmptok == "Float") {
485 string tmptok = lex.getString();
486 inset.reset(new InsetFloat(buf.params(), tmptok));
487 } else if (tmptok == "Wrap") {
489 string tmptok = lex.getString();
490 inset.reset(new InsetWrap(buf.params(), tmptok));
492 } else if (tmptok == "List") {
493 inset.reset(new InsetList);
494 } else if (tmptok == "Theorem") {
495 inset.reset(new InsetList);
497 } else if (tmptok == "Caption") {
498 inset.reset(new InsetCaption(buf.params()));
499 } else if (tmptok == "Index") {
500 inset.reset(new InsetIndex(buf.params()));
501 } else if (tmptok == "FloatList") {
502 inset.reset(new InsetFloatList);
503 } else if (tmptok == "Info") {
504 inset.reset(new InsetInfo(buf.params()));
506 lyxerr << "unknown Inset type '" << tmptok
508 while (lex.isOK() && lex.getString() != "\\end_inset")
513 inset->read(buf, lex);
516 if (inset->lyxCode() == MATHMACRO_CODE) {
517 MathMacroTemplate const * tmpl =
518 static_cast<MathMacroTemplate*>(inset.get());
519 MacroTable::globalMacros().insert
520 (tmpl->name(), tmpl->asMacroData());
522 << BOOST_CURRENT_FUNCTION
523 << ": creating local macro " << to_utf8(tmpl->name())
528 return inset.release();