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/insetnomencl.h"
41 #include "insets/insetlabel.h"
42 #include "insets/insetline.h"
43 #include "insets/insetmarginal.h"
44 #include "insets/insetnote.h"
45 #include "insets/insetbox.h"
46 #include "insets/insetbranch.h"
47 #include "insets/insetoptarg.h"
48 #include "insets/insetpagebreak.h"
49 #include "insets/insetref.h"
50 #include "insets/insetspace.h"
51 #include "insets/insettabular.h"
52 #include "insets/insettoc.h"
53 #include "insets/inseturl.h"
54 #include "insets/insetvspace.h"
55 #include "insets/insetwrap.h"
57 #include "mathed/MathMacroTemplate.h"
58 #include "mathed/InsetMathHull.h"
60 #include "support/lstrings.h"
62 #include <boost/assert.hpp>
63 #include <boost/current_function.hpp>
70 using support::compare_ascii_no_case;
77 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
79 BufferParams const & params = bv->buffer()->params();
82 case LFUN_HFILL_INSERT:
83 return new InsetHFill;
85 case LFUN_LINE_INSERT:
88 case LFUN_PAGEBREAK_INSERT:
89 return new InsetPagebreak;
91 case LFUN_CLEARPAGE_INSERT:
92 return new InsetClearPage;
94 case LFUN_CLEARDOUBLEPAGE_INSERT:
95 return new InsetClearDoublePage;
97 case LFUN_CHARSTYLE_INSERT: {
98 string s = cmd.getArg(0);
99 LyXTextClass tclass = params.getLyXTextClass();
100 CharStyles::iterator found_cs = tclass.charstyle(s);
101 if (found_cs != tclass.charstyles().end())
102 return new InsetCharStyle(params, found_cs);
104 return new InsetCharStyle(params, s);
107 case LFUN_NOTE_INSERT: {
108 string arg = cmd.getArg(0);
111 return new InsetNote(params, arg);
114 case LFUN_BOX_INSERT: {
115 string arg = cmd.getArg(0);
118 return new InsetBox(params, arg);
121 case LFUN_BRANCH_INSERT: {
122 docstring arg = cmd.argument();
124 arg = from_ascii("none");
125 return new InsetBranch(params, InsetBranchParams(arg));
128 case LFUN_ERT_INSERT:
129 return new InsetERT(params);
131 case LFUN_FOOTNOTE_INSERT:
132 return new InsetFoot(params);
134 case LFUN_MARGINALNOTE_INSERT:
135 return new InsetMarginal(params);
137 case LFUN_OPTIONAL_INSERT:
138 return new InsetOptArg(params);
140 case LFUN_BIBITEM_INSERT:
141 return new InsetBibitem(InsetCommandParams("bibitem"));
143 case LFUN_FLOAT_INSERT: {
144 // check if the float type exists
145 string const argument = to_utf8(cmd.argument());
146 if (params.getLyXTextClass().floats().typeExist(argument))
147 return new InsetFloat(params, argument);
148 lyxerr << "Non-existent float type: " << argument << endl;
152 case LFUN_FLOAT_WIDE_INSERT: {
153 // check if the float type exists
154 string const argument = to_utf8(cmd.argument());
155 if (params.getLyXTextClass().floats().typeExist(argument)) {
156 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
157 p->wide(true, params);
160 lyxerr << "Non-existent float type: " << argument << endl;
164 case LFUN_WRAP_INSERT: {
165 string const argument = to_utf8(cmd.argument());
166 if (argument == "figure")
167 return new InsetWrap(params, argument);
168 lyxerr << "Non-existent floatflt type: " << argument << endl;
172 case LFUN_INDEX_INSERT: {
173 // Try and generate a valid index entry.
174 InsetCommandParams icp("index");
175 icp["name"] = cmd.argument().empty() ?
176 bv->getLyXText()->getStringToIndex(bv->cursor()) :
178 return new InsetIndex(icp);
181 case LFUN_NOMENCL_INSERT: {
182 InsetCommandParams icp("nomenclature");
183 icp["symbol"] = cmd.argument().empty() ?
184 bv->getLyXText()->getStringToIndex(bv->cursor()) :
186 return new InsetNomencl(icp);
189 case LFUN_TABULAR_INSERT: {
190 if (cmd.argument().empty())
192 std::istringstream ss(to_utf8(cmd.argument()));
199 return new InsetTabular(*bv->buffer(), r, c);
202 case LFUN_CAPTION_INSERT: {
203 auto_ptr<InsetCaption> inset(new InsetCaption(params));
204 inset->setAutoBreakRows(true);
205 inset->setDrawFrame(true);
206 inset->setFrameColor(LColor::captionframe);
207 return inset.release();
210 case LFUN_INDEX_PRINT:
211 return new InsetPrintIndex(InsetCommandParams("printindex"));
213 case LFUN_NOMENCL_PRINT:
214 return new InsetPrintNomencl(InsetCommandParams("printnomenclature"));
216 case LFUN_TOC_INSERT:
217 return new InsetTOC(InsetCommandParams("tableofcontents"));
219 case LFUN_ENVIRONMENT_INSERT:
220 return new InsetEnvironment(params, to_utf8(cmd.argument()));
223 case LFUN_LIST_INSERT:
224 return new InsetList;
226 case LFUN_THEOREM_INSERT:
227 return new InsetTheorem;
230 case LFUN_INSET_INSERT: {
231 string const name = cmd.getArg(0);
233 if (name == "bibitem") {
234 InsetCommandParams icp(name);
235 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
237 return new InsetBibitem(icp);
239 } else if (name == "bibtex") {
240 InsetCommandParams icp(name);
241 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
243 return new InsetBibtex(icp);
245 } else if (name == "citation") {
246 InsetCommandParams icp("cite");
247 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
249 return new InsetCitation(icp);
251 } else if (name == "ert") {
252 InsetCollapsable::CollapseStatus st;
253 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
254 return new InsetERT(params, st);
256 } else if (name == "external") {
257 Buffer const & buffer = *bv->buffer();
258 InsetExternalParams iep;
259 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
261 auto_ptr<InsetExternal> inset(new InsetExternal);
262 inset->setParams(iep, buffer);
263 return inset.release();
265 } else if (name == "graphics") {
266 Buffer const & buffer = *bv->buffer();
267 InsetGraphicsParams igp;
268 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
270 auto_ptr<InsetGraphics> inset(new InsetGraphics);
271 inset->setParams(igp);
272 return inset.release();
274 } else if (name == "include") {
275 InsetCommandParams iip(name);
276 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
277 return new InsetInclude(iip);
279 } else if (name == "index") {
280 InsetCommandParams icp(name);
281 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
283 return new InsetIndex(icp);
285 } else if (name == "nomenclature") {
286 InsetCommandParams icp(name);
287 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
289 return new InsetNomencl(icp);
291 } else if (name == "label") {
292 InsetCommandParams icp(name);
293 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
295 return new InsetLabel(icp);
297 } else if (name == "ref") {
298 InsetCommandParams icp(name);
299 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
301 return new InsetRef(icp, *bv->buffer());
303 } else if (name == "toc") {
304 InsetCommandParams icp("tableofcontents");
305 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
307 return new InsetTOC(icp);
309 } else if (name == "url") {
310 InsetCommandParams icp(name);
311 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
313 return new InsetUrl(icp);
315 } else if (name == "vspace") {
317 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
318 return new InsetVSpace(vspace);
322 case LFUN_SPACE_INSERT: {
323 string const name = to_utf8(cmd.argument());
324 if (name == "normal")
325 return new InsetSpace(InsetSpace::NORMAL);
326 else if (name == "protected")
327 return new InsetSpace(InsetSpace::PROTECTED);
328 else if (name == "thin")
329 return new InsetSpace(InsetSpace::THIN);
330 else if (name == "quad")
331 return new InsetSpace(InsetSpace::QUAD);
332 else if (name == "qquad")
333 return new InsetSpace(InsetSpace::QQUAD);
334 else if (name == "enspace")
335 return new InsetSpace(InsetSpace::ENSPACE);
336 else if (name == "enskip")
337 return new InsetSpace(InsetSpace::ENSKIP);
338 else if (name == "negthinspace")
339 return new InsetSpace(InsetSpace::NEGTHIN);
340 else if (name.empty())
341 lyxerr << "LyX function 'space' needs an argument." << endl;
343 lyxerr << "Wrong argument for LyX function 'space'." << endl;
356 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
359 if (lex.getString() != "\\begin_inset") {
360 lyxerr << "Buffer::readInset: Consistency check failed."
364 auto_ptr<InsetBase> inset;
366 LyXTextClass tclass = buf.params().getLyXTextClass();
369 string tmptok = lex.getString();
371 // test the different insets
372 if (tmptok == "LatexCommand") {
374 string const cmdName = lex.getString();
375 lex.pushToken(cmdName);
377 InsetCommandParams inscmd(cmdName);
380 // This strange command allows LyX to recognize "natbib" style
381 // citations: citet, citep, Citet etc.
382 // FIXME: We already have partial support for \\fullcite and
383 // the various \\footcite commands. We should increase the
384 // file format number and read these commands here, too.
385 // Then we should use is_possible_cite_command() in
386 // src/frontends/controllers/biblio.C to test for valid cite
388 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
389 inset.reset(new InsetCitation(inscmd));
390 } else if (cmdName == "bibitem") {
391 inset.reset(new InsetBibitem(inscmd));
392 } else if (cmdName == "bibtex") {
393 inset.reset(new InsetBibtex(inscmd));
394 } else if (cmdName == "index") {
395 inset.reset(new InsetIndex(inscmd));
396 } else if (cmdName == "nomenclature") {
397 inset.reset(new InsetNomencl(inscmd));
398 } else if (cmdName == "include") {
399 inset.reset(new InsetInclude(inscmd));
400 } else if (cmdName == "label") {
401 inset.reset(new InsetLabel(inscmd));
402 } else if (cmdName == "url"
403 || cmdName == "htmlurl") {
404 inset.reset(new InsetUrl(inscmd));
405 } else if (cmdName == "ref"
406 || cmdName == "eqref"
407 || cmdName == "pageref"
409 || cmdName == "vpageref"
410 || cmdName == "prettyref") {
411 if (!inscmd["name"].empty()
412 || !inscmd["reference"].empty()) {
413 inset.reset(new InsetRef(inscmd, buf));
415 } else if (cmdName == "tableofcontents") {
416 inset.reset(new InsetTOC(inscmd));
417 } else if (cmdName == "listofalgorithms") {
418 inset.reset(new InsetFloatList("algorithm"));
419 } else if (cmdName == "listoffigures") {
420 inset.reset(new InsetFloatList("figure"));
421 } else if (cmdName == "listoftables") {
422 inset.reset(new InsetFloatList("table"));
423 } else if (cmdName == "printindex") {
424 inset.reset(new InsetPrintIndex(inscmd));
425 } else if (cmdName == "printnomenclature") {
426 inset.reset(new InsetPrintNomencl(inscmd));
428 lyxerr << "unknown CommandInset '" << cmdName
430 while (lex.isOK() && lex.getString() != "\\end_inset")
435 if (tmptok == "Quotes") {
436 inset.reset(new InsetQuotes);
437 } else if (tmptok == "External") {
438 inset.reset(new InsetExternal);
439 } else if (tmptok == "FormulaMacro") {
440 inset.reset(new MathMacroTemplate);
441 } else if (tmptok == "Formula") {
442 inset.reset(new InsetMathHull);
443 } else if (tmptok == "Graphics") {
444 inset.reset(new InsetGraphics);
445 } else if (tmptok == "Note") {
446 inset.reset(new InsetNote(buf.params(), tmptok));
447 } else if (tmptok == "Box") {
448 inset.reset(new InsetBox(buf.params(), tmptok));
449 } else if (tmptok == "CharStyle") {
451 string s = lex.getString();
452 CharStyles::iterator found_cs = tclass.charstyle(s);
453 if (found_cs != tclass.charstyles().end())
454 inset.reset(new InsetCharStyle(buf.params(), found_cs));
457 inset.reset(new InsetCharStyle(buf.params(), s));
459 } else if (tmptok == "Branch") {
460 inset.reset(new InsetBranch(buf.params(),
461 InsetBranchParams()));
462 } else if (tmptok == "Include") {
463 InsetCommandParams p("include");
464 inset.reset(new InsetInclude(p));
465 } else if (tmptok == "Environment") {
467 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
468 } else if (tmptok == "ERT") {
469 inset.reset(new InsetERT(buf.params()));
470 } else if (tmptok == "InsetSpace") {
471 inset.reset(new InsetSpace);
472 } else if (tmptok == "Tabular") {
473 inset.reset(new InsetTabular(buf));
474 } else if (tmptok == "Text") {
475 inset.reset(new InsetText(buf.params()));
476 } else if (tmptok == "VSpace") {
477 inset.reset(new InsetVSpace);
478 } else if (tmptok == "Foot") {
479 inset.reset(new InsetFoot(buf.params()));
480 } else if (tmptok == "Marginal") {
481 inset.reset(new InsetMarginal(buf.params()));
482 } else if (tmptok == "OptArg") {
483 inset.reset(new InsetOptArg(buf.params()));
484 } else if (tmptok == "Float") {
486 string tmptok = lex.getString();
487 inset.reset(new InsetFloat(buf.params(), tmptok));
488 } else if (tmptok == "Wrap") {
490 string tmptok = lex.getString();
491 inset.reset(new InsetWrap(buf.params(), tmptok));
493 } else if (tmptok == "List") {
494 inset.reset(new InsetList);
495 } else if (tmptok == "Theorem") {
496 inset.reset(new InsetList);
498 } else if (tmptok == "Caption") {
499 inset.reset(new InsetCaption(buf.params()));
500 } else if (tmptok == "FloatList") {
501 inset.reset(new InsetFloatList);
503 lyxerr << "unknown Inset type '" << tmptok
505 while (lex.isOK() && lex.getString() != "\\end_inset")
510 inset->read(buf, lex);
515 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
516 MathMacroTemplate const * tmpl =
517 static_cast<MathMacroTemplate*>(inset.get());
518 MacroTable::globalMacros().insert
519 (tmpl->name(), tmpl->asMacroData());
521 << BOOST_CURRENT_FUNCTION
522 << ": creating local macro " << to_utf8(tmpl->name())
527 return inset.release();