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>
66 using lyx::support::compare_ascii_no_case;
73 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
75 BufferParams const & params = bv->buffer()->params();
78 case LFUN_HFILL_INSERT:
79 return new InsetHFill;
81 case LFUN_LINE_INSERT:
84 case LFUN_PAGEBREAK_INSERT:
85 return new InsetPagebreak;
87 case LFUN_CHARSTYLE_INSERT: {
88 string s = cmd.getArg(0);
89 LyXTextClass tclass = params.getLyXTextClass();
90 CharStyles::iterator found_cs = tclass.charstyle(s);
91 if (found_cs != tclass.charstyles().end())
92 return new InsetCharStyle(params, found_cs);
94 return new InsetCharStyle(params, s);
97 case LFUN_NOTE_INSERT: {
98 string arg = cmd.getArg(0);
101 return new InsetNote(params, arg);
104 case LFUN_BOX_INSERT: {
105 string arg = cmd.getArg(0);
108 return new InsetBox(params, arg);
111 case LFUN_BRANCH_INSERT: {
112 string arg = cmd.getArg(0);
115 return new InsetBranch(params, InsetBranchParams(arg));
118 case LFUN_ERT_INSERT:
119 return new InsetERT(params);
121 case LFUN_FOOTNOTE_INSERT:
122 return new InsetFoot(params);
124 case LFUN_MARGINALNOTE_INSERT:
125 return new InsetMarginal(params);
127 case LFUN_OPTIONAL_INSERT:
128 return new InsetOptArg(params);
130 case LFUN_BIBITEM_INSERT:
131 return new InsetBibitem(InsetCommandParams("bibitem"));
133 case LFUN_FLOAT_INSERT: {
134 // check if the float type exists
135 string const argument = lyx::to_utf8(cmd.argument());
136 if (params.getLyXTextClass().floats().typeExist(argument))
137 return new InsetFloat(params, argument);
138 lyxerr << "Non-existent float type: " << argument << endl;
142 case LFUN_FLOAT_WIDE_INSERT: {
143 // check if the float type exists
144 string const argument = lyx::to_utf8(cmd.argument());
145 if (params.getLyXTextClass().floats().typeExist(argument)) {
146 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
147 p->wide(true, params);
150 lyxerr << "Non-existent float type: " << argument << endl;
154 case LFUN_WRAP_INSERT: {
155 string const argument = lyx::to_utf8(cmd.argument());
156 if (argument == "figure")
157 return new InsetWrap(params, argument);
158 lyxerr << "Non-existent floatflt type: " << argument << endl;
162 case LFUN_INDEX_INSERT: {
163 // Try and generate a valid index entry.
164 InsetCommandParams icp("index");
165 string const contents = cmd.argument().empty() ?
166 bv->getLyXText()->getStringToIndex(bv->cursor()) :
167 lyx::to_utf8(cmd.argument());
168 icp.setContents(contents);
169 return new InsetIndex(icp);
172 case LFUN_TABULAR_INSERT: {
173 if (cmd.argument().empty())
175 std::istringstream ss(lyx::to_utf8(cmd.argument()));
182 return new InsetTabular(*bv->buffer(), r, c);
185 case LFUN_CAPTION_INSERT: {
186 auto_ptr<InsetCaption> inset(new InsetCaption(params));
187 inset->setAutoBreakRows(true);
188 inset->setDrawFrame(true);
189 inset->setFrameColor(LColor::captionframe);
190 return inset.release();
193 case LFUN_INDEX_PRINT:
194 return new InsetPrintIndex(InsetCommandParams("printindex"));
196 case LFUN_TOC_INSERT:
197 return new InsetTOC(InsetCommandParams("tableofcontents"));
199 case LFUN_ENVIRONMENT_INSERT:
200 return new InsetEnvironment(params, lyx::to_utf8(cmd.argument()));
203 case LFUN_LIST_INSERT:
204 return new InsetList;
206 case LFUN_THEOREM_INSERT:
207 return new InsetTheorem;
210 case LFUN_INSET_INSERT: {
211 string const name = cmd.getArg(0);
213 if (name == "bibitem") {
214 InsetCommandParams icp;
215 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
217 return new InsetBibitem(icp);
219 } else if (name == "bibtex") {
220 InsetCommandParams icp;
221 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
223 return new InsetBibtex(icp);
225 } else if (name == "citation") {
226 InsetCommandParams icp;
227 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
229 return new InsetCitation(icp);
231 } else if (name == "ert") {
232 InsetCollapsable::CollapseStatus st;
233 InsetERTMailer::string2params(lyx::to_utf8(cmd.argument()), st);
234 return new InsetERT(params, st);
236 } else if (name == "external") {
237 Buffer const & buffer = *bv->buffer();
238 InsetExternalParams iep;
239 InsetExternalMailer::string2params(lyx::to_utf8(cmd.argument()),
241 auto_ptr<InsetExternal> inset(new InsetExternal);
242 inset->setParams(iep, buffer);
243 return inset.release();
245 } else if (name == "graphics") {
246 Buffer const & buffer = *bv->buffer();
247 InsetGraphicsParams igp;
248 InsetGraphicsMailer::string2params(lyx::to_utf8(cmd.argument()),
250 auto_ptr<InsetGraphics> inset(new InsetGraphics);
251 inset->setParams(igp);
252 return inset.release();
254 } else if (name == "include") {
255 InsetCommandParams iip;
256 InsetIncludeMailer::string2params(lyx::to_utf8(cmd.argument()), iip);
257 return new InsetInclude(iip);
259 } else if (name == "index") {
260 InsetCommandParams icp;
261 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
263 return new InsetIndex(icp);
265 } else if (name == "label") {
266 InsetCommandParams icp;
267 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
269 return new InsetLabel(icp);
271 } else if (name == "ref") {
272 InsetCommandParams icp;
273 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
275 return new InsetRef(icp, *bv->buffer());
277 } else if (name == "toc") {
278 InsetCommandParams icp;
279 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
281 return new InsetTOC(icp);
283 } else if (name == "url") {
284 InsetCommandParams icp;
285 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
287 return new InsetUrl(icp);
289 } else if (name == "vspace") {
291 InsetVSpaceMailer::string2params(lyx::to_utf8(cmd.argument()), vspace);
292 return new InsetVSpace(vspace);
296 case LFUN_SPACE_INSERT: {
297 string const name = lyx::to_utf8(cmd.argument());
298 if (name == "normal")
299 return new InsetSpace(InsetSpace::NORMAL);
300 else if (name == "protected")
301 return new InsetSpace(InsetSpace::PROTECTED);
302 else if (name == "thin")
303 return new InsetSpace(InsetSpace::THIN);
304 else if (name == "quad")
305 return new InsetSpace(InsetSpace::QUAD);
306 else if (name == "qquad")
307 return new InsetSpace(InsetSpace::QQUAD);
308 else if (name == "enspace")
309 return new InsetSpace(InsetSpace::ENSPACE);
310 else if (name == "enskip")
311 return new InsetSpace(InsetSpace::ENSKIP);
312 else if (name == "negthinspace")
313 return new InsetSpace(InsetSpace::NEGTHIN);
314 else if (name.empty())
315 lyxerr << "LyX function 'space' needs an argument." << endl;
317 lyxerr << "Wrong argument for LyX function 'space'." << endl;
330 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
333 if (lex.getString() != "\\begin_inset") {
334 lyxerr << "Buffer::readInset: Consistency check failed."
338 auto_ptr<InsetBase> inset;
340 LyXTextClass tclass = buf.params().getLyXTextClass();
343 string tmptok = lex.getString();
345 // test the different insets
346 if (tmptok == "LatexCommand") {
347 InsetCommandParams inscmd;
350 string const cmdName = inscmd.getCmdName();
352 // This strange command allows LyX to recognize "natbib" style
353 // citations: citet, citep, Citet etc.
354 // FIXME: We already have partial support for \\fullcite and
355 // the various \\footcite commands. We should increase the
356 // file format number and read these commands here, too.
357 // Then we should use is_possible_cite_command() in
358 // src/frontends/controllers/biblio.C to test for valid cite
360 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
361 inset.reset(new InsetCitation(inscmd));
362 } else if (cmdName == "bibitem") {
363 lex.printError("Wrong place for bibitem");
364 inset.reset(new InsetBibitem(inscmd));
365 } else if (cmdName == "bibtex") {
366 inset.reset(new InsetBibtex(inscmd));
367 } else if (cmdName == "index") {
368 inset.reset(new InsetIndex(inscmd));
369 } else if (cmdName == "include") {
370 inset.reset(new InsetInclude(inscmd));
371 } else if (cmdName == "label") {
372 inset.reset(new InsetLabel(inscmd));
373 } else if (cmdName == "url"
374 || cmdName == "htmlurl") {
375 inset.reset(new InsetUrl(inscmd));
376 } else if (cmdName == "ref"
377 || cmdName == "eqref"
378 || cmdName == "pageref"
380 || cmdName == "vpageref"
381 || cmdName == "prettyref") {
382 if (!inscmd.getOptions().empty()
383 || !inscmd.getContents().empty()) {
384 inset.reset(new InsetRef(inscmd, buf));
386 } else if (cmdName == "tableofcontents") {
387 inset.reset(new InsetTOC(inscmd));
388 } else if (cmdName == "listofalgorithms") {
389 inset.reset(new InsetFloatList("algorithm"));
390 } else if (cmdName == "listoffigures") {
391 inset.reset(new InsetFloatList("figure"));
392 } else if (cmdName == "listoftables") {
393 inset.reset(new InsetFloatList("table"));
394 } else if (cmdName == "printindex") {
395 inset.reset(new InsetPrintIndex(inscmd));
397 lyxerr << "unknown CommandInset '" << cmdName
399 while (lex.isOK() && lex.getString() != "\\end_inset")
404 if (tmptok == "Quotes") {
405 inset.reset(new InsetQuotes);
406 } else if (tmptok == "External") {
407 inset.reset(new InsetExternal);
408 } else if (tmptok == "FormulaMacro") {
409 inset.reset(new MathMacroTemplate);
410 } else if (tmptok == "Formula") {
411 inset.reset(new InsetMathHull);
412 } else if (tmptok == "Graphics") {
413 inset.reset(new InsetGraphics);
414 } else if (tmptok == "Note") {
415 inset.reset(new InsetNote(buf.params(), tmptok));
416 } else if (tmptok == "Box") {
417 inset.reset(new InsetBox(buf.params(), tmptok));
418 } else if (tmptok == "CharStyle") {
420 string s = lex.getString();
421 CharStyles::iterator found_cs = tclass.charstyle(s);
422 if (found_cs != tclass.charstyles().end())
423 inset.reset(new InsetCharStyle(buf.params(), found_cs));
426 inset.reset(new InsetCharStyle(buf.params(), s));
428 } else if (tmptok == "Branch") {
429 inset.reset(new InsetBranch(buf.params(),
430 InsetBranchParams()));
431 } else if (tmptok == "Include") {
432 InsetCommandParams p("Include");
433 inset.reset(new InsetInclude(p));
434 } else if (tmptok == "Environment") {
436 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
437 } else if (tmptok == "ERT") {
438 inset.reset(new InsetERT(buf.params()));
439 } else if (tmptok == "InsetSpace") {
440 inset.reset(new InsetSpace);
441 } else if (tmptok == "Tabular") {
442 inset.reset(new InsetTabular(buf));
443 } else if (tmptok == "Text") {
444 inset.reset(new InsetText(buf.params()));
445 } else if (tmptok == "VSpace") {
446 inset.reset(new InsetVSpace);
447 } else if (tmptok == "Foot") {
448 inset.reset(new InsetFoot(buf.params()));
449 } else if (tmptok == "Marginal") {
450 inset.reset(new InsetMarginal(buf.params()));
451 } else if (tmptok == "OptArg") {
452 inset.reset(new InsetOptArg(buf.params()));
453 } else if (tmptok == "Float") {
455 string tmptok = lex.getString();
456 inset.reset(new InsetFloat(buf.params(), tmptok));
457 } else if (tmptok == "Wrap") {
459 string tmptok = lex.getString();
460 inset.reset(new InsetWrap(buf.params(), tmptok));
462 } else if (tmptok == "List") {
463 inset.reset(new InsetList);
464 } else if (tmptok == "Theorem") {
465 inset.reset(new InsetList);
467 } else if (tmptok == "Caption") {
468 inset.reset(new InsetCaption(buf.params()));
469 } else if (tmptok == "FloatList") {
470 inset.reset(new InsetFloatList);
472 lyxerr << "unknown Inset type '" << tmptok
474 while (lex.isOK() && lex.getString() != "\\end_inset")
479 inset->read(buf, lex);
484 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
485 MathMacroTemplate const * tmpl =
486 static_cast<MathMacroTemplate*>(inset.get());
487 MacroTable::globalMacros().insert
488 (tmpl->name(), tmpl->asMacroData());
490 << BOOST_CURRENT_FUNCTION
491 << ": creating local macro " << tmpl->name()
496 return inset.release();