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/MathMacroTemplate.h"
57 #include "mathed/InsetMathHull.h"
59 #include "support/lstrings.h"
61 #include <boost/assert.hpp>
62 #include <boost/current_function.hpp>
69 using support::compare_ascii_no_case;
76 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
78 BufferParams const & params = bv->buffer()->params();
81 case LFUN_HFILL_INSERT:
82 return new InsetHFill;
84 case LFUN_LINE_INSERT:
87 case LFUN_PAGEBREAK_INSERT:
88 return new InsetPagebreak;
90 case LFUN_CHARSTYLE_INSERT: {
91 string s = cmd.getArg(0);
92 LyXTextClass tclass = params.getLyXTextClass();
93 CharStyles::iterator found_cs = tclass.charstyle(s);
94 if (found_cs != tclass.charstyles().end())
95 return new InsetCharStyle(params, found_cs);
97 return new InsetCharStyle(params, s);
100 case LFUN_NOTE_INSERT: {
101 string arg = cmd.getArg(0);
104 return new InsetNote(params, arg);
107 case LFUN_BOX_INSERT: {
108 string arg = cmd.getArg(0);
111 return new InsetBox(params, arg);
114 case LFUN_BRANCH_INSERT: {
115 string arg = cmd.getArg(0);
118 return new InsetBranch(params, InsetBranchParams(arg));
121 case LFUN_ERT_INSERT:
122 return new InsetERT(params);
124 case LFUN_FOOTNOTE_INSERT:
125 return new InsetFoot(params);
127 case LFUN_MARGINALNOTE_INSERT:
128 return new InsetMarginal(params);
130 case LFUN_OPTIONAL_INSERT:
131 return new InsetOptArg(params);
133 case LFUN_BIBITEM_INSERT:
134 return new InsetBibitem(InsetCommandParams("bibitem"));
136 case LFUN_FLOAT_INSERT: {
137 // check if the float type exists
138 string const argument = to_utf8(cmd.argument());
139 if (params.getLyXTextClass().floats().typeExist(argument))
140 return new InsetFloat(params, argument);
141 lyxerr << "Non-existent float type: " << argument << endl;
145 case LFUN_FLOAT_WIDE_INSERT: {
146 // check if the float type exists
147 string const argument = to_utf8(cmd.argument());
148 if (params.getLyXTextClass().floats().typeExist(argument)) {
149 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
150 p->wide(true, params);
153 lyxerr << "Non-existent float type: " << argument << endl;
157 case LFUN_WRAP_INSERT: {
158 string const argument = to_utf8(cmd.argument());
159 if (argument == "figure")
160 return new InsetWrap(params, argument);
161 lyxerr << "Non-existent floatflt type: " << argument << endl;
165 case LFUN_INDEX_INSERT: {
166 // Try and generate a valid index entry.
167 InsetCommandParams icp("index");
168 icp["name"] = cmd.argument().empty() ?
169 from_utf8(bv->getLyXText()->getStringToIndex(bv->cursor())) :
171 return new InsetIndex(icp);
174 case LFUN_TABULAR_INSERT: {
175 if (cmd.argument().empty())
177 std::istringstream ss(to_utf8(cmd.argument()));
184 return new InsetTabular(*bv->buffer(), r, c);
187 case LFUN_CAPTION_INSERT: {
188 auto_ptr<InsetCaption> inset(new InsetCaption(params));
189 inset->setAutoBreakRows(true);
190 inset->setDrawFrame(true);
191 inset->setFrameColor(LColor::captionframe);
192 return inset.release();
195 case LFUN_INDEX_PRINT:
196 return new InsetPrintIndex(InsetCommandParams("printindex"));
198 case LFUN_TOC_INSERT:
199 return new InsetTOC(InsetCommandParams("tableofcontents"));
201 case LFUN_ENVIRONMENT_INSERT:
202 return new InsetEnvironment(params, to_utf8(cmd.argument()));
205 case LFUN_LIST_INSERT:
206 return new InsetList;
208 case LFUN_THEOREM_INSERT:
209 return new InsetTheorem;
212 case LFUN_INSET_INSERT: {
213 string const name = cmd.getArg(0);
215 if (name == "bibitem") {
216 InsetCommandParams icp(name);
217 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
219 return new InsetBibitem(icp);
221 } else if (name == "bibtex") {
222 InsetCommandParams icp(name);
223 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
225 return new InsetBibtex(icp);
227 } else if (name == "citation") {
228 InsetCommandParams icp("cite");
229 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
231 return new InsetCitation(icp);
233 } else if (name == "ert") {
234 InsetCollapsable::CollapseStatus st;
235 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
236 return new InsetERT(params, st);
238 } else if (name == "external") {
239 Buffer const & buffer = *bv->buffer();
240 InsetExternalParams iep;
241 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
243 auto_ptr<InsetExternal> inset(new InsetExternal);
244 inset->setParams(iep, buffer);
245 return inset.release();
247 } else if (name == "graphics") {
248 Buffer const & buffer = *bv->buffer();
249 InsetGraphicsParams igp;
250 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
252 auto_ptr<InsetGraphics> inset(new InsetGraphics);
253 inset->setParams(igp);
254 return inset.release();
256 } else if (name == "include") {
257 InsetCommandParams iip(name);
258 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
259 return new InsetInclude(iip);
261 } else if (name == "index") {
262 InsetCommandParams icp(name);
263 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
265 return new InsetIndex(icp);
267 } else if (name == "label") {
268 InsetCommandParams icp(name);
269 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
271 return new InsetLabel(icp);
273 } else if (name == "ref") {
274 InsetCommandParams icp(name);
275 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
277 return new InsetRef(icp, *bv->buffer());
279 } else if (name == "toc") {
280 InsetCommandParams icp("tableofcontents");
281 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
283 return new InsetTOC(icp);
285 } else if (name == "url") {
286 InsetCommandParams icp(name);
287 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
289 return new InsetUrl(icp);
291 } else if (name == "vspace") {
293 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
294 return new InsetVSpace(vspace);
298 case LFUN_SPACE_INSERT: {
299 string const name = to_utf8(cmd.argument());
300 if (name == "normal")
301 return new InsetSpace(InsetSpace::NORMAL);
302 else if (name == "protected")
303 return new InsetSpace(InsetSpace::PROTECTED);
304 else if (name == "thin")
305 return new InsetSpace(InsetSpace::THIN);
306 else if (name == "quad")
307 return new InsetSpace(InsetSpace::QUAD);
308 else if (name == "qquad")
309 return new InsetSpace(InsetSpace::QQUAD);
310 else if (name == "enspace")
311 return new InsetSpace(InsetSpace::ENSPACE);
312 else if (name == "enskip")
313 return new InsetSpace(InsetSpace::ENSKIP);
314 else if (name == "negthinspace")
315 return new InsetSpace(InsetSpace::NEGTHIN);
316 else if (name.empty())
317 lyxerr << "LyX function 'space' needs an argument." << endl;
319 lyxerr << "Wrong argument for LyX function 'space'." << endl;
332 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
335 if (lex.getString() != "\\begin_inset") {
336 lyxerr << "Buffer::readInset: Consistency check failed."
340 auto_ptr<InsetBase> inset;
342 LyXTextClass tclass = buf.params().getLyXTextClass();
345 string tmptok = lex.getString();
347 // test the different insets
348 if (tmptok == "LatexCommand") {
350 string const cmdName = lex.getString();
351 lex.pushToken(cmdName);
353 InsetCommandParams inscmd(cmdName);
356 // This strange command allows LyX to recognize "natbib" style
357 // citations: citet, citep, Citet etc.
358 // FIXME: We already have partial support for \\fullcite and
359 // the various \\footcite commands. We should increase the
360 // file format number and read these commands here, too.
361 // Then we should use is_possible_cite_command() in
362 // src/frontends/controllers/biblio.C to test for valid cite
364 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
365 inset.reset(new InsetCitation(inscmd));
366 } else if (cmdName == "bibitem") {
367 inset.reset(new InsetBibitem(inscmd));
368 } else if (cmdName == "bibtex") {
369 inset.reset(new InsetBibtex(inscmd));
370 } else if (cmdName == "index") {
371 inset.reset(new InsetIndex(inscmd));
372 } else if (cmdName == "include") {
373 inset.reset(new InsetInclude(inscmd));
374 } else if (cmdName == "label") {
375 inset.reset(new InsetLabel(inscmd));
376 } else if (cmdName == "url"
377 || cmdName == "htmlurl") {
378 inset.reset(new InsetUrl(inscmd));
379 } else if (cmdName == "ref"
380 || cmdName == "eqref"
381 || cmdName == "pageref"
383 || cmdName == "vpageref"
384 || cmdName == "prettyref") {
385 if (!inscmd["name"].empty()
386 || !inscmd["reference"].empty()) {
387 inset.reset(new InsetRef(inscmd, buf));
389 } else if (cmdName == "tableofcontents") {
390 inset.reset(new InsetTOC(inscmd));
391 } else if (cmdName == "listofalgorithms") {
392 inset.reset(new InsetFloatList("algorithm"));
393 } else if (cmdName == "listoffigures") {
394 inset.reset(new InsetFloatList("figure"));
395 } else if (cmdName == "listoftables") {
396 inset.reset(new InsetFloatList("table"));
397 } else if (cmdName == "printindex") {
398 inset.reset(new InsetPrintIndex(inscmd));
400 lyxerr << "unknown CommandInset '" << cmdName
402 while (lex.isOK() && lex.getString() != "\\end_inset")
407 if (tmptok == "Quotes") {
408 inset.reset(new InsetQuotes);
409 } else if (tmptok == "External") {
410 inset.reset(new InsetExternal);
411 } else if (tmptok == "FormulaMacro") {
412 inset.reset(new MathMacroTemplate);
413 } else if (tmptok == "Formula") {
414 inset.reset(new InsetMathHull);
415 } else if (tmptok == "Graphics") {
416 inset.reset(new InsetGraphics);
417 } else if (tmptok == "Note") {
418 inset.reset(new InsetNote(buf.params(), tmptok));
419 } else if (tmptok == "Box") {
420 inset.reset(new InsetBox(buf.params(), tmptok));
421 } else if (tmptok == "CharStyle") {
423 string s = lex.getString();
424 CharStyles::iterator found_cs = tclass.charstyle(s);
425 if (found_cs != tclass.charstyles().end())
426 inset.reset(new InsetCharStyle(buf.params(), found_cs));
429 inset.reset(new InsetCharStyle(buf.params(), s));
431 } else if (tmptok == "Branch") {
432 inset.reset(new InsetBranch(buf.params(),
433 InsetBranchParams()));
434 } else if (tmptok == "Include") {
435 InsetCommandParams p("Include");
436 inset.reset(new InsetInclude(p));
437 } else if (tmptok == "Environment") {
439 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
440 } else if (tmptok == "ERT") {
441 inset.reset(new InsetERT(buf.params()));
442 } else if (tmptok == "InsetSpace") {
443 inset.reset(new InsetSpace);
444 } else if (tmptok == "Tabular") {
445 inset.reset(new InsetTabular(buf));
446 } else if (tmptok == "Text") {
447 inset.reset(new InsetText(buf.params()));
448 } else if (tmptok == "VSpace") {
449 inset.reset(new InsetVSpace);
450 } else if (tmptok == "Foot") {
451 inset.reset(new InsetFoot(buf.params()));
452 } else if (tmptok == "Marginal") {
453 inset.reset(new InsetMarginal(buf.params()));
454 } else if (tmptok == "OptArg") {
455 inset.reset(new InsetOptArg(buf.params()));
456 } else if (tmptok == "Float") {
458 string tmptok = lex.getString();
459 inset.reset(new InsetFloat(buf.params(), tmptok));
460 } else if (tmptok == "Wrap") {
462 string tmptok = lex.getString();
463 inset.reset(new InsetWrap(buf.params(), tmptok));
465 } else if (tmptok == "List") {
466 inset.reset(new InsetList);
467 } else if (tmptok == "Theorem") {
468 inset.reset(new InsetList);
470 } else if (tmptok == "Caption") {
471 inset.reset(new InsetCaption(buf.params()));
472 } else if (tmptok == "FloatList") {
473 inset.reset(new InsetFloatList);
475 lyxerr << "unknown Inset type '" << tmptok
477 while (lex.isOK() && lex.getString() != "\\end_inset")
482 inset->read(buf, lex);
487 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
488 MathMacroTemplate const * tmpl =
489 static_cast<MathMacroTemplate*>(inset.get());
490 MacroTable::globalMacros().insert
491 (tmpl->name(), tmpl->asMacroData());
493 << BOOST_CURRENT_FUNCTION
494 << ": creating local macro " << to_utf8(tmpl->name())
499 return inset.release();