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);
162 return new InsetIndex(icp);
165 case LFUN_TABULAR_INSERT: {
166 if (cmd.argument.empty())
168 std::istringstream ss(cmd.argument);
175 return new InsetTabular(*bv->buffer(), r, c);
178 case LFUN_INSET_CAPTION: {
179 auto_ptr<InsetCaption> inset(new InsetCaption(params));
180 inset->setAutoBreakRows(true);
181 inset->setDrawFrame(true);
182 inset->setFrameColor(LColor::captionframe);
183 return inset.release();
186 case LFUN_INDEX_PRINT:
187 return new InsetPrintIndex(InsetCommandParams("printindex"));
189 case LFUN_TOC_INSERT:
190 return new InsetTOC(InsetCommandParams("tableofcontents"));
192 case LFUN_ENVIRONMENT_INSERT:
193 return new InsetEnvironment(params, cmd.argument);
196 case LFUN_INSET_LIST:
197 return new InsetList;
199 case LFUN_INSET_THEOREM:
200 return new InsetTheorem;
203 case LFUN_INSET_INSERT: {
204 string const name = cmd.getArg(0);
206 if (name == "bibitem") {
207 InsetCommandParams icp;
208 InsetCommandMailer::string2params(name, cmd.argument,
210 return new InsetBibitem(icp);
212 } else if (name == "bibtex") {
213 InsetCommandParams icp;
214 InsetCommandMailer::string2params(name, cmd.argument,
216 return new InsetBibtex(icp);
218 } else if (name == "citation") {
219 InsetCommandParams icp;
220 InsetCommandMailer::string2params(name, cmd.argument,
222 return new InsetCitation(icp);
224 } else if (name == "ert") {
225 InsetCollapsable::CollapseStatus st;
226 InsetERTMailer::string2params(cmd.argument, st);
227 return new InsetERT(params, st);
229 } else if (name == "external") {
230 Buffer const & buffer = *bv->buffer();
231 InsetExternalParams iep;
232 InsetExternalMailer::string2params(cmd.argument,
234 auto_ptr<InsetExternal> inset(new InsetExternal);
235 inset->setParams(iep, buffer);
236 return inset.release();
238 } else if (name == "graphics") {
239 Buffer const & buffer = *bv->buffer();
240 InsetGraphicsParams igp;
241 InsetGraphicsMailer::string2params(cmd.argument,
243 auto_ptr<InsetGraphics> inset(new InsetGraphics);
244 inset->setParams(igp);
245 return inset.release();
247 } else if (name == "include") {
248 InsetCommandParams iip;
249 InsetIncludeMailer::string2params(cmd.argument, iip);
250 return new InsetInclude(iip);
252 } else if (name == "index") {
253 InsetCommandParams icp;
254 InsetCommandMailer::string2params(name, cmd.argument,
256 return new InsetIndex(icp);
258 } else if (name == "label") {
259 InsetCommandParams icp;
260 InsetCommandMailer::string2params(name, cmd.argument,
262 return new InsetLabel(icp);
264 } else if (name == "ref") {
265 InsetCommandParams icp;
266 InsetCommandMailer::string2params(name, cmd.argument,
268 return new InsetRef(icp, *bv->buffer());
270 } else if (name == "toc") {
271 InsetCommandParams icp;
272 InsetCommandMailer::string2params(name, cmd.argument,
274 return new InsetTOC(icp);
276 } else if (name == "url") {
277 InsetCommandParams icp;
278 InsetCommandMailer::string2params(name, cmd.argument,
280 return new InsetUrl(icp);
282 } else if (name == "vspace") {
284 InsetVSpaceMailer::string2params(cmd.argument, vspace);
285 return new InsetVSpace(vspace);
289 case LFUN_SPACE_INSERT: {
290 string const name = cmd.argument;
291 if (name == "normal")
292 return new InsetSpace(InsetSpace::NORMAL);
293 else if (name == "protected")
294 return new InsetSpace(InsetSpace::PROTECTED);
295 else if (name == "thin")
296 return new InsetSpace(InsetSpace::THIN);
297 else if (name == "quad")
298 return new InsetSpace(InsetSpace::QUAD);
299 else if (name == "qquad")
300 return new InsetSpace(InsetSpace::QQUAD);
301 else if (name == "enspace")
302 return new InsetSpace(InsetSpace::ENSPACE);
303 else if (name == "enskip")
304 return new InsetSpace(InsetSpace::ENSKIP);
305 else if (name == "negthinspace")
306 return new InsetSpace(InsetSpace::NEGTHIN);
307 else if (name.empty())
308 lyxerr << "LyX function 'space' needs an argument." << endl;
310 lyxerr << "Wrong argument for LyX function 'space'." << endl;
323 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
326 if (lex.getString() != "\\begin_inset") {
327 lyxerr << "Buffer::readInset: Consistency check failed."
331 auto_ptr<InsetBase> inset;
333 LyXTextClass tclass = buf.params().getLyXTextClass();
336 string tmptok = lex.getString();
338 // test the different insets
339 if (tmptok == "LatexCommand") {
340 InsetCommandParams inscmd;
343 string const cmdName = inscmd.getCmdName();
345 // This strange command allows LyX to recognize "natbib" style
346 // citations: citet, citep, Citet etc.
347 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
348 inset.reset(new InsetCitation(inscmd));
349 } else if (cmdName == "bibitem") {
350 lex.printError("Wrong place for bibitem");
351 inset.reset(new InsetBibitem(inscmd));
352 } else if (cmdName == "bibtex") {
353 inset.reset(new InsetBibtex(inscmd));
354 } else if (cmdName == "index") {
355 inset.reset(new InsetIndex(inscmd));
356 } else if (cmdName == "include") {
357 inset.reset(new InsetInclude(inscmd));
358 } else if (cmdName == "label") {
359 inset.reset(new InsetLabel(inscmd));
360 } else if (cmdName == "url"
361 || cmdName == "htmlurl") {
362 inset.reset(new InsetUrl(inscmd));
363 } else if (cmdName == "ref"
364 || cmdName == "eqref"
365 || cmdName == "pageref"
367 || cmdName == "vpageref"
368 || cmdName == "prettyref") {
369 if (!inscmd.getOptions().empty()
370 || !inscmd.getContents().empty()) {
371 inset.reset(new InsetRef(inscmd, buf));
373 } else if (cmdName == "tableofcontents") {
374 inset.reset(new InsetTOC(inscmd));
375 } else if (cmdName == "listofalgorithms") {
376 inset.reset(new InsetFloatList("algorithm"));
377 } else if (cmdName == "listoffigures") {
378 inset.reset(new InsetFloatList("figure"));
379 } else if (cmdName == "listoftables") {
380 inset.reset(new InsetFloatList("table"));
381 } else if (cmdName == "printindex") {
382 inset.reset(new InsetPrintIndex(inscmd));
384 lyxerr << "unknown CommandInset '" << cmdName
386 while (lex.isOK() && lex.getString() != "\\end_inset")
391 if (tmptok == "Quotes") {
392 inset.reset(new InsetQuotes);
393 } else if (tmptok == "External") {
394 inset.reset(new InsetExternal);
395 } else if (tmptok == "FormulaMacro") {
396 inset.reset(new MathMacroTemplate);
397 } else if (tmptok == "Formula") {
398 inset.reset(new MathHullInset);
399 } else if (tmptok == "Graphics") {
400 inset.reset(new InsetGraphics);
401 } else if (tmptok == "Note") {
402 inset.reset(new InsetNote(buf.params(), tmptok));
403 } else if (tmptok == "Box") {
404 inset.reset(new InsetBox(buf.params(), tmptok));
405 } else if (tmptok == "CharStyle") {
407 string s = lex.getString();
408 CharStyles::iterator found_cs = tclass.charstyle(s);
409 if (found_cs != tclass.charstyles().end())
410 inset.reset(new InsetCharStyle(buf.params(), found_cs));
413 inset.reset(new InsetCharStyle(buf.params(), s));
415 } else if (tmptok == "Branch") {
416 inset.reset(new InsetBranch(buf.params(),
417 InsetBranchParams()));
418 } else if (tmptok == "Include") {
419 InsetCommandParams p("Include");
420 inset.reset(new InsetInclude(p));
421 } else if (tmptok == "Environment") {
423 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
424 } else if (tmptok == "ERT") {
425 inset.reset(new InsetERT(buf.params()));
426 } else if (tmptok == "InsetSpace") {
427 inset.reset(new InsetSpace);
428 } else if (tmptok == "Tabular") {
429 inset.reset(new InsetTabular(buf));
430 } else if (tmptok == "Text") {
431 inset.reset(new InsetText(buf.params()));
432 } else if (tmptok == "VSpace") {
433 inset.reset(new InsetVSpace);
434 } else if (tmptok == "Foot") {
435 inset.reset(new InsetFoot(buf.params()));
436 } else if (tmptok == "Marginal") {
437 inset.reset(new InsetMarginal(buf.params()));
438 } else if (tmptok == "OptArg") {
439 inset.reset(new InsetOptArg(buf.params()));
440 } else if (tmptok == "Float") {
442 string tmptok = lex.getString();
443 inset.reset(new InsetFloat(buf.params(), tmptok));
444 } else if (tmptok == "Wrap") {
446 string tmptok = lex.getString();
447 inset.reset(new InsetWrap(buf.params(), tmptok));
449 } else if (tmptok == "List") {
450 inset.reset(new InsetList);
451 } else if (tmptok == "Theorem") {
452 inset.reset(new InsetList);
454 } else if (tmptok == "Caption") {
455 inset.reset(new InsetCaption(buf.params()));
456 } else if (tmptok == "FloatList") {
457 inset.reset(new InsetFloatList);
459 lyxerr << "unknown Inset type '" << tmptok
461 while (lex.isOK() && lex.getString() != "\\end_inset")
466 inset->read(buf, lex);
471 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
472 MathMacroTemplate const * tmpl =
473 static_cast<MathMacroTemplate*>(inset.get());
474 MacroTable::globalMacros().insert
475 (tmpl->name(), tmpl->asMacroData());
477 << BOOST_CURRENT_FUNCTION
478 << ": creating local macro " << tmpl->name()
483 return inset.release();