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"
23 #include "paragraph.h"
25 #include "insets/insetbibitem.h"
26 #include "insets/insetbibtex.h"
27 #include "insets/insetcaption.h"
28 #include "insets/insetcite.h"
29 #include "insets/insetcharstyle.h"
30 #include "insets/insetenv.h"
31 #include "insets/insetert.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/insetlabel.h"
41 #include "insets/insetline.h"
42 #include "insets/insetmarginal.h"
43 #include "insets/insetnote.h"
44 #include "insets/insetbox.h"
45 #include "insets/insetbranch.h"
46 #include "insets/insetoptarg.h"
47 #include "insets/insetpagebreak.h"
48 #include "insets/insetref.h"
49 #include "insets/insetspace.h"
50 #include "insets/insettabular.h"
51 #include "insets/insettoc.h"
52 #include "insets/inseturl.h"
53 #include "insets/insetvspace.h"
54 #include "insets/insetwrap.h"
56 #include "mathed/math_macrotemplate.h"
57 #include "mathed/math_hullinset.h"
59 #include "support/lstrings.h"
61 #include <boost/assert.hpp>
65 using lyx::support::compare_ascii_no_case;
72 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
74 BufferParams const & params = bv->buffer()->params();
78 return new InsetHFill;
80 case LFUN_INSERT_LINE:
83 case LFUN_INSERT_PAGEBREAK:
84 return new InsetPagebreak;
86 case LFUN_INSERT_CHARSTYLE: {
87 string s = cmd.getArg(0);
88 LyXTextClass tclass = params.getLyXTextClass();
89 CharStyles::iterator found_cs = tclass.charstyle(s);
90 if (found_cs != tclass.charstyles().end())
91 return new InsetCharStyle(params, found_cs);
93 return new InsetCharStyle(params, s);
96 case LFUN_INSERT_NOTE: {
97 string arg = cmd.getArg(0);
100 return new InsetNote(params, arg);
103 case LFUN_INSERT_BOX: {
104 string arg = cmd.getArg(0);
107 return new InsetBox(params, arg);
110 case LFUN_INSERT_BRANCH: {
111 string arg = cmd.getArg(0);
114 return new InsetBranch(params, InsetBranchParams(arg));
118 return new InsetERT(params);
120 case LFUN_INSET_FOOTNOTE:
121 return new InsetFoot(params);
123 case LFUN_INSET_MARGINAL:
124 return new InsetMarginal(params);
126 case LFUN_INSET_OPTARG:
127 return new InsetOptArg(params);
129 case LFUN_INSERT_BIBITEM:
130 return new InsetBibitem(InsetCommandParams("bibitem"));
132 case LFUN_INSET_FLOAT:
133 // check if the float type exists
134 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
135 return new InsetFloat(params, cmd.argument);
136 lyxerr << "Non-existent float type: " << cmd.argument << endl;
139 case LFUN_INSET_WIDE_FLOAT:
140 // check if the float type exists
141 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
142 auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
143 p->wide(true, params);
146 lyxerr << "Non-existent float type: " << cmd.argument << endl;
149 case LFUN_INSET_WRAP:
150 if (cmd.argument == "figure")
151 return new InsetWrap(params, cmd.argument);
152 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
155 case LFUN_INDEX_INSERT: {
156 // Try and generate a valid index entry.
157 InsetCommandParams icp("index");
158 string const contents = cmd.argument.empty() ?
159 bv->getLyXText()->getStringToIndex(bv->cursor()) :
161 icp.setContents(contents);
163 return new InsetIndex(icp);
167 case LFUN_TABULAR_INSERT:
168 if (!cmd.argument.empty()) {
169 std::istringstream ss(cmd.argument);
174 return new InsetTabular(*bv->buffer(), r, c);
178 case LFUN_INSET_CAPTION: {
179 UpdatableInset * up = bv->cursor().inset().asUpdatableInset();
181 auto_ptr<InsetCaption> inset(new InsetCaption(params));
182 inset->setAutoBreakRows(true);
183 inset->setDrawFrame(true);
184 inset->setFrameColor(LColor::captionframe);
185 return inset.release();
190 case LFUN_INDEX_PRINT:
191 return new InsetPrintIndex(InsetCommandParams("printindex"));
193 case LFUN_TOC_INSERT:
194 return new InsetTOC(InsetCommandParams("tableofcontents"));
196 case LFUN_ENVIRONMENT_INSERT:
197 return new InsetEnvironment(params, cmd.argument);
200 case LFUN_INSET_LIST:
201 return new InsetList;
203 case LFUN_INSET_THEOREM:
204 return new InsetTheorem;
207 case LFUN_INSET_INSERT: {
208 string const name = cmd.getArg(0);
210 if (name == "bibitem") {
211 InsetCommandParams icp;
212 InsetCommandMailer::string2params(name, cmd.argument,
214 return new InsetBibitem(icp);
216 } else if (name == "bibtex") {
217 InsetCommandParams icp;
218 InsetCommandMailer::string2params(name, cmd.argument,
220 return new InsetBibtex(icp);
222 } else if (name == "citation") {
223 InsetCommandParams icp;
224 InsetCommandMailer::string2params(name, cmd.argument,
226 return new InsetCitation(icp);
228 } else if (name == "ert") {
229 InsetCollapsable::CollapseStatus st;
230 InsetERTMailer::string2params(cmd.argument, st);
231 return new InsetERT(params, st);
233 } else if (name == "external") {
234 Buffer const & buffer = *bv->buffer();
235 InsetExternalParams iep;
236 InsetExternalMailer::string2params(cmd.argument,
238 auto_ptr<InsetExternal> inset(new InsetExternal);
239 inset->setParams(iep, buffer);
240 return inset.release();
242 } else if (name == "graphics") {
243 Buffer const & buffer = *bv->buffer();
244 InsetGraphicsParams igp;
245 InsetGraphicsMailer::string2params(cmd.argument,
247 auto_ptr<InsetGraphics> inset(new InsetGraphics);
248 inset->setParams(igp);
249 return inset.release();
251 } else if (name == "include") {
252 InsetCommandParams iip;
253 InsetIncludeMailer::string2params(cmd.argument, iip);
254 return new InsetInclude(iip);
256 } else if (name == "index") {
257 InsetCommandParams icp;
258 InsetCommandMailer::string2params(name, cmd.argument,
260 return new InsetIndex(icp);
262 } else if (name == "label") {
263 InsetCommandParams icp;
264 InsetCommandMailer::string2params(name, cmd.argument,
266 return new InsetLabel(icp);
268 } else if (name == "ref") {
269 InsetCommandParams icp;
270 InsetCommandMailer::string2params(name, cmd.argument,
272 return new InsetRef(icp, *bv->buffer());
274 } else if (name == "toc") {
275 InsetCommandParams icp;
276 InsetCommandMailer::string2params(name, cmd.argument,
278 return new InsetTOC(icp);
280 } else if (name == "url") {
281 InsetCommandParams icp;
282 InsetCommandMailer::string2params(name, cmd.argument,
284 return new InsetUrl(icp);
286 } else if (name == "vspace") {
288 InsetVSpaceMailer::string2params(cmd.argument, vspace);
289 return new InsetVSpace(vspace);
293 case LFUN_SPACE_INSERT: {
294 string const name = cmd.argument;
295 if (name == "normal")
296 return new InsetSpace(InsetSpace::NORMAL);
297 else if (name == "protected")
298 return new InsetSpace(InsetSpace::PROTECTED);
299 else if (name == "thin")
300 return new InsetSpace(InsetSpace::THIN);
301 else if (name == "quad")
302 return new InsetSpace(InsetSpace::QUAD);
303 else if (name == "qquad")
304 return new InsetSpace(InsetSpace::QQUAD);
305 else if (name == "enspace")
306 return new InsetSpace(InsetSpace::ENSPACE);
307 else if (name == "enskip")
308 return new InsetSpace(InsetSpace::ENSKIP);
309 else if (name == "negthinspace")
310 return new InsetSpace(InsetSpace::NEGTHIN);
311 else if (name.empty())
312 lyxerr << "LyX function 'space' needs an argument." << endl;
314 lyxerr << "Wrong argument for LyX function 'space'." << endl;
327 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
330 if (lex.getString() != "\\begin_inset") {
331 lyxerr << "Buffer::readInset: Consistency check failed."
335 auto_ptr<InsetBase> inset;
337 LyXTextClass tclass = buf.params().getLyXTextClass();
340 string tmptok = lex.getString();
342 // test the different insets
343 if (tmptok == "LatexCommand") {
344 InsetCommandParams inscmd;
347 string const cmdName = inscmd.getCmdName();
349 // This strange command allows LyX to recognize "natbib" style
350 // citations: citet, citep, Citet etc.
351 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
352 inset.reset(new InsetCitation(inscmd));
353 } else if (cmdName == "bibitem") {
354 lex.printError("Wrong place for bibitem");
355 inset.reset(new InsetBibitem(inscmd));
356 } else if (cmdName == "bibtex") {
357 inset.reset(new InsetBibtex(inscmd));
358 } else if (cmdName == "index") {
359 inset.reset(new InsetIndex(inscmd));
360 } else if (cmdName == "include") {
361 inset.reset(new InsetInclude(inscmd));
362 } else if (cmdName == "label") {
363 inset.reset(new InsetLabel(inscmd));
364 } else if (cmdName == "url"
365 || cmdName == "htmlurl") {
366 inset.reset(new InsetUrl(inscmd));
367 } else if (cmdName == "ref"
368 || cmdName == "eqref"
369 || cmdName == "pageref"
371 || cmdName == "vpageref"
372 || cmdName == "prettyref") {
373 if (!inscmd.getOptions().empty()
374 || !inscmd.getContents().empty()) {
375 inset.reset(new InsetRef(inscmd, buf));
377 } else if (cmdName == "tableofcontents") {
378 inset.reset(new InsetTOC(inscmd));
379 } else if (cmdName == "listofalgorithms") {
380 inset.reset(new InsetFloatList("algorithm"));
381 } else if (cmdName == "listoffigures") {
382 inset.reset(new InsetFloatList("figure"));
383 } else if (cmdName == "listoftables") {
384 inset.reset(new InsetFloatList("table"));
385 } else if (cmdName == "printindex") {
386 inset.reset(new InsetPrintIndex(inscmd));
388 lyxerr << "unknown CommandInset '" << cmdName
390 while (lex.isOK() && lex.getString() != "\\end_inset")
395 if (tmptok == "Quotes") {
396 inset.reset(new InsetQuotes);
397 } else if (tmptok == "External") {
398 inset.reset(new InsetExternal);
399 } else if (tmptok == "FormulaMacro") {
400 inset.reset(new MathMacroTemplate);
401 } else if (tmptok == "Formula") {
402 inset.reset(new MathHullInset);
403 } else if (tmptok == "Graphics") {
404 inset.reset(new InsetGraphics);
405 } else if (tmptok == "Note") {
406 inset.reset(new InsetNote(buf.params(), tmptok));
407 } else if (tmptok == "Box") {
408 inset.reset(new InsetBox(buf.params(), tmptok));
409 } else if (tmptok == "CharStyle") {
411 string s = lex.getString();
412 CharStyles::iterator found_cs = tclass.charstyle(s);
413 if (found_cs != tclass.charstyles().end())
414 inset.reset(new InsetCharStyle(buf.params(), found_cs));
417 inset.reset(new InsetCharStyle(buf.params(), s));
419 } else if (tmptok == "Branch") {
420 inset.reset(new InsetBranch(buf.params(),
421 InsetBranchParams()));
422 } else if (tmptok == "Include") {
423 InsetCommandParams p("Include");
424 inset.reset(new InsetInclude(p));
425 } else if (tmptok == "Environment") {
427 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
428 } else if (tmptok == "ERT") {
429 inset.reset(new InsetERT(buf.params()));
430 } else if (tmptok == "InsetSpace") {
431 inset.reset(new InsetSpace);
432 } else if (tmptok == "Tabular") {
433 inset.reset(new InsetTabular(buf));
434 } else if (tmptok == "Text") {
435 inset.reset(new InsetText(buf.params()));
436 } else if (tmptok == "VSpace") {
437 inset.reset(new InsetVSpace);
438 } else if (tmptok == "Foot") {
439 inset.reset(new InsetFoot(buf.params()));
440 } else if (tmptok == "Marginal") {
441 inset.reset(new InsetMarginal(buf.params()));
442 } else if (tmptok == "OptArg") {
443 inset.reset(new InsetOptArg(buf.params()));
444 } else if (tmptok == "Float") {
446 string tmptok = lex.getString();
447 inset.reset(new InsetFloat(buf.params(), tmptok));
448 } else if (tmptok == "Wrap") {
450 string tmptok = lex.getString();
451 inset.reset(new InsetWrap(buf.params(), tmptok));
453 } else if (tmptok == "List") {
454 inset.reset(new InsetList);
455 } else if (tmptok == "Theorem") {
456 inset.reset(new InsetList);
458 } else if (tmptok == "Caption") {
459 inset.reset(new InsetCaption(buf.params()));
460 } else if (tmptok == "FloatList") {
461 inset.reset(new InsetFloatList);
463 lyxerr << "unknown Inset type '" << tmptok
465 while (lex.isOK() && lex.getString() != "\\end_inset")
470 inset->read(buf, lex);
475 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
476 MathMacroTemplate const * tmpl =
477 static_cast<MathMacroTemplate*>(inset.get());
478 MacroTable::globalMacros().insert
479 (tmpl->name(), tmpl->asMacroData());
480 lyxerr << "creating local macro " << tmpl->name() << endl;
484 return inset.release();