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 "frontends/Dialogs.h"
60 #include "frontends/LyXView.h"
62 #include "support/lstrings.h"
64 #include <boost/assert.hpp>
68 using lyx::support::compare_ascii_no_case;
75 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
77 BufferParams const & params = bv->buffer()->params();
81 return new InsetHFill;
83 case LFUN_INSERT_LINE:
86 case LFUN_INSERT_PAGEBREAK:
87 return new InsetPagebreak;
89 case LFUN_INSERT_CHARSTYLE: {
90 string s = cmd.getArg(0);
91 LyXTextClass tclass = params.getLyXTextClass();
92 CharStyles::iterator found_cs = tclass.charstyle(s);
93 if (found_cs != tclass.charstyles().end())
94 return new InsetCharStyle(params, found_cs);
96 return new InsetCharStyle(params, s);
99 case LFUN_INSERT_NOTE: {
100 string arg = cmd.getArg(0);
103 return new InsetNote(params, arg);
106 case LFUN_INSERT_BOX: {
107 string arg = cmd.getArg(0);
110 return new InsetBox(params, arg);
113 case LFUN_INSERT_BRANCH: {
114 string arg = cmd.getArg(0);
117 return new InsetBranch(params, InsetBranchParams(arg));
121 return new InsetERT(params);
123 case LFUN_INSET_FOOTNOTE:
124 return new InsetFoot(params);
126 case LFUN_INSET_MARGINAL:
127 return new InsetMarginal(params);
129 case LFUN_INSET_OPTARG:
130 return new InsetOptArg(params);
132 case LFUN_INSERT_BIBITEM:
133 return new InsetBibitem(InsetCommandParams("bibitem"));
135 case LFUN_INSET_FLOAT:
136 // check if the float type exists
137 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
138 return new InsetFloat(params, cmd.argument);
139 lyxerr << "Non-existent float type: " << cmd.argument << endl;
142 case LFUN_INSET_WIDE_FLOAT:
143 // check if the float type exists
144 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
145 auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
146 p->wide(true, params);
149 lyxerr << "Non-existent float type: " << cmd.argument << endl;
152 case LFUN_INSET_WRAP:
153 if (cmd.argument == "figure")
154 return new InsetWrap(params, cmd.argument);
155 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
158 case LFUN_INDEX_INSERT: {
159 // Try and generate a valid index entry.
160 InsetCommandParams icp("index");
161 string const contents = cmd.argument.empty() ?
162 bv->getLyXText()->getStringToIndex(bv->cursor()) :
164 icp.setContents(contents);
166 string data = InsetCommandMailer::params2string("index", icp);
167 LyXView * lv = bv->owner();
169 if (icp.getContents().empty()) {
170 lv->getDialogs().show("index", data, 0);
172 lv->dispatch(FuncRequest(LFUN_INSET_APPLY, data));
177 case LFUN_TABULAR_INSERT:
178 if (!cmd.argument.empty()) {
179 std::istringstream ss(cmd.argument);
184 return new InsetTabular(*bv->buffer(), r, c);
186 bv->owner()->getDialogs().show("tabularcreate");
189 case LFUN_INSET_CAPTION: {
190 UpdatableInset * up = bv->cursor().inset().asUpdatableInset();
192 auto_ptr<InsetCaption> inset(new InsetCaption(params));
193 inset->setAutoBreakRows(true);
194 inset->setDrawFrame(true);
195 inset->setFrameColor(LColor::captionframe);
196 return inset.release();
201 case LFUN_INDEX_PRINT:
202 return new InsetPrintIndex(InsetCommandParams("printindex"));
204 case LFUN_TOC_INSERT:
205 return new InsetTOC(InsetCommandParams("tableofcontents"));
207 case LFUN_ENVIRONMENT_INSERT:
208 return new InsetEnvironment(params, cmd.argument);
211 case LFUN_INSET_LIST:
212 return new InsetList;
214 case LFUN_INSET_THEOREM:
215 return new InsetTheorem;
218 case LFUN_INSET_INSERT: {
219 string const name = cmd.getArg(0);
221 if (name == "bibitem") {
222 InsetCommandParams icp;
223 InsetCommandMailer::string2params(name, cmd.argument,
225 return new InsetBibitem(icp);
227 } else if (name == "bibtex") {
228 InsetCommandParams icp;
229 InsetCommandMailer::string2params(name, cmd.argument,
231 return new InsetBibtex(icp);
233 } else if (name == "citation") {
234 InsetCommandParams icp;
235 InsetCommandMailer::string2params(name, cmd.argument,
237 return new InsetCitation(icp);
239 } else if (name == "ert") {
240 InsetCollapsable::CollapseStatus st;
241 InsetERTMailer::string2params(cmd.argument, st);
242 return new InsetERT(params, st);
244 } else if (name == "external") {
245 Buffer const & buffer = *bv->buffer();
246 InsetExternalParams iep;
247 InsetExternalMailer::string2params(cmd.argument,
249 auto_ptr<InsetExternal> inset(new InsetExternal);
250 inset->setParams(iep, buffer);
251 return inset.release();
253 } else if (name == "graphics") {
254 Buffer const & buffer = *bv->buffer();
255 InsetGraphicsParams igp;
256 InsetGraphicsMailer::string2params(cmd.argument,
258 auto_ptr<InsetGraphics> inset(new InsetGraphics);
259 inset->setParams(igp);
260 return inset.release();
262 } else if (name == "include") {
263 InsetCommandParams iip;
264 InsetIncludeMailer::string2params(cmd.argument, iip);
265 return new InsetInclude(iip);
267 } else if (name == "index") {
268 InsetCommandParams icp;
269 InsetCommandMailer::string2params(name, cmd.argument,
271 return new InsetIndex(icp);
273 } else if (name == "label") {
274 InsetCommandParams icp;
275 InsetCommandMailer::string2params(name, cmd.argument,
277 return new InsetLabel(icp);
279 } else if (name == "ref") {
280 InsetCommandParams icp;
281 InsetCommandMailer::string2params(name, cmd.argument,
283 return new InsetRef(icp, *bv->buffer());
285 } else if (name == "toc") {
286 InsetCommandParams icp;
287 InsetCommandMailer::string2params(name, cmd.argument,
289 return new InsetTOC(icp);
291 } else if (name == "url") {
292 InsetCommandParams icp;
293 InsetCommandMailer::string2params(name, cmd.argument,
295 return new InsetUrl(icp);
297 } else if (name == "vspace") {
299 InsetVSpaceMailer::string2params(cmd.argument, vspace);
300 return new InsetVSpace(vspace);
304 case LFUN_SPACE_INSERT: {
305 string const name = cmd.argument;
306 if (name == "normal")
307 return new InsetSpace(InsetSpace::NORMAL);
308 else if (name == "protected")
309 return new InsetSpace(InsetSpace::PROTECTED);
310 else if (name == "thin")
311 return new InsetSpace(InsetSpace::THIN);
312 else if (name == "quad")
313 return new InsetSpace(InsetSpace::QUAD);
314 else if (name == "qquad")
315 return new InsetSpace(InsetSpace::QQUAD);
316 else if (name == "enspace")
317 return new InsetSpace(InsetSpace::ENSPACE);
318 else if (name == "enskip")
319 return new InsetSpace(InsetSpace::ENSKIP);
320 else if (name == "negthinspace")
321 return new InsetSpace(InsetSpace::NEGTHIN);
322 else if (name.empty())
323 lyxerr << "LyX function 'space' needs an argument." << endl;
325 lyxerr << "Wrong argument for LyX function 'space'." << endl;
338 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
341 if (lex.getString() != "\\begin_inset") {
342 lyxerr << "Buffer::readInset: Consistency check failed."
346 auto_ptr<InsetBase> inset;
348 LyXTextClass tclass = buf.params().getLyXTextClass();
351 string tmptok = lex.getString();
353 // test the different insets
354 if (tmptok == "LatexCommand") {
355 InsetCommandParams inscmd;
358 string const cmdName = inscmd.getCmdName();
360 // This strange command allows LyX to recognize "natbib" style
361 // citations: citet, citep, Citet etc.
362 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
363 inset.reset(new InsetCitation(inscmd));
364 } else if (cmdName == "bibitem") {
365 lex.printError("Wrong place for bibitem");
366 inset.reset(new InsetBibitem(inscmd));
367 } else if (cmdName == "bibtex") {
368 inset.reset(new InsetBibtex(inscmd));
369 } else if (cmdName == "index") {
370 inset.reset(new InsetIndex(inscmd));
371 } else if (cmdName == "include") {
372 inset.reset(new InsetInclude(inscmd));
373 } else if (cmdName == "label") {
374 inset.reset(new InsetLabel(inscmd));
375 } else if (cmdName == "url"
376 || cmdName == "htmlurl") {
377 inset.reset(new InsetUrl(inscmd));
378 } else if (cmdName == "ref"
379 || cmdName == "eqref"
380 || cmdName == "pageref"
382 || cmdName == "vpageref"
383 || cmdName == "prettyref") {
384 if (!inscmd.getOptions().empty()
385 || !inscmd.getContents().empty()) {
386 inset.reset(new InsetRef(inscmd, buf));
388 } else if (cmdName == "tableofcontents") {
389 inset.reset(new InsetTOC(inscmd));
390 } else if (cmdName == "listofalgorithms") {
391 inset.reset(new InsetFloatList("algorithm"));
392 } else if (cmdName == "listoffigures") {
393 inset.reset(new InsetFloatList("figure"));
394 } else if (cmdName == "listoftables") {
395 inset.reset(new InsetFloatList("table"));
396 } else if (cmdName == "printindex") {
397 inset.reset(new InsetPrintIndex(inscmd));
399 lyxerr << "unknown CommandInset '" << cmdName
401 while (lex.isOK() && lex.getString() != "\\end_inset")
406 if (tmptok == "Quotes") {
407 inset.reset(new InsetQuotes);
408 } else if (tmptok == "External") {
409 inset.reset(new InsetExternal);
410 } else if (tmptok == "FormulaMacro") {
411 inset.reset(new MathMacroTemplate);
412 } else if (tmptok == "Formula") {
413 inset.reset(new MathHullInset);
414 } else if (tmptok == "Graphics") {
415 inset.reset(new InsetGraphics);
416 } else if (tmptok == "Note") {
417 inset.reset(new InsetNote(buf.params(), tmptok));
418 } else if (tmptok == "Box") {
419 inset.reset(new InsetBox(buf.params(), tmptok));
420 } else if (tmptok == "CharStyle") {
422 string s = lex.getString();
423 CharStyles::iterator found_cs = tclass.charstyle(s);
424 if (found_cs != tclass.charstyles().end())
425 inset.reset(new InsetCharStyle(buf.params(), found_cs));
428 inset.reset(new InsetCharStyle(buf.params(), s));
430 } else if (tmptok == "Branch") {
431 inset.reset(new InsetBranch(buf.params(),
432 InsetBranchParams()));
433 } else if (tmptok == "Include") {
434 InsetCommandParams p("Include");
435 inset.reset(new InsetInclude(p));
436 } else if (tmptok == "Environment") {
438 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
439 } else if (tmptok == "ERT") {
440 inset.reset(new InsetERT(buf.params()));
441 } else if (tmptok == "InsetSpace") {
442 inset.reset(new InsetSpace);
443 } else if (tmptok == "Tabular") {
444 inset.reset(new InsetTabular(buf));
445 } else if (tmptok == "Text") {
446 inset.reset(new InsetText(buf.params()));
447 } else if (tmptok == "VSpace") {
448 inset.reset(new InsetVSpace);
449 } else if (tmptok == "Foot") {
450 inset.reset(new InsetFoot(buf.params()));
451 } else if (tmptok == "Marginal") {
452 inset.reset(new InsetMarginal(buf.params()));
453 } else if (tmptok == "OptArg") {
454 inset.reset(new InsetOptArg(buf.params()));
455 } else if (tmptok == "Float") {
457 string tmptok = lex.getString();
458 inset.reset(new InsetFloat(buf.params(), tmptok));
459 } else if (tmptok == "Wrap") {
461 string tmptok = lex.getString();
462 inset.reset(new InsetWrap(buf.params(), tmptok));
464 } else if (tmptok == "List") {
465 inset.reset(new InsetList);
466 } else if (tmptok == "Theorem") {
467 inset.reset(new InsetList);
469 } else if (tmptok == "Caption") {
470 inset.reset(new InsetCaption(buf.params()));
471 } else if (tmptok == "FloatList") {
472 inset.reset(new InsetFloatList);
474 lyxerr << "unknown Inset type '" << tmptok
476 while (lex.isOK() && lex.getString() != "\\end_inset")
481 inset->read(buf, lex);
486 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
487 MathMacroTemplate const * tmpl =
488 static_cast<MathMacroTemplate*>(inset.get());
489 MacroTable::globalMacros().insert
490 (tmpl->name(), tmpl->asMacroData());
491 lyxerr << "creating local macro " << tmpl->name() << endl;
495 return inset.release();