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"
24 #include "insets/insetbibitem.h"
25 #include "insets/insetbibtex.h"
26 #include "insets/insetcaption.h"
27 #include "insets/insetcite.h"
28 #include "insets/insetenv.h"
29 #include "insets/insetert.h"
30 #include "insets/insetexternal.h"
31 #include "insets/insetfloat.h"
32 #include "insets/insetfloatlist.h"
33 #include "insets/insetfoot.h"
34 #include "insets/insetgraphics.h"
35 #include "insets/insethfill.h"
36 #include "insets/insetinclude.h"
37 #include "insets/insetindex.h"
38 #include "insets/insetlabel.h"
39 #include "insets/insetmarginal.h"
40 #include "insets/insetminipage.h"
41 #include "insets/insetnote.h"
42 #include "insets/insetbranch.h"
43 #include "insets/insetoptarg.h"
44 #include "insets/insetref.h"
45 #include "insets/insetspace.h"
46 #include "insets/insettabular.h"
47 #include "insets/insettoc.h"
48 #include "insets/inseturl.h"
49 #include "insets/insetwrap.h"
50 #include "mathed/formulamacro.h"
51 #include "mathed/formula.h"
53 #include "frontends/Dialogs.h"
54 #include "frontends/LyXView.h"
55 #include "support/lstrings.h"
56 #include "support/std_sstream.h"
58 #include <boost/assert.hpp>
60 using lyx::support::compare_ascii_no_case;
66 InsetOld * createInset(FuncRequest const & cmd)
68 BufferView * bv = cmd.view();
69 BufferParams const & params = bv->buffer()->params();
73 return new InsetHFill;
75 case LFUN_INSET_MINIPAGE:
76 return new InsetMinipage(params);
78 case LFUN_INSERT_NOTE:
80 string arg = cmd.getArg(0);
83 return new InsetNote(params, arg);
85 case LFUN_INSERT_BRANCH:
87 string arg = cmd.getArg(0);
90 return new InsetBranch(params, arg);
94 return new InsetERT(params);
96 case LFUN_INSET_FOOTNOTE:
97 return new InsetFoot(params);
99 case LFUN_INSET_MARGINAL:
100 return new InsetMarginal(params);
102 case LFUN_INSET_OPTARG:
103 return new InsetOptArg(params);
105 case LFUN_INSERT_BIBITEM:
106 return new InsetBibitem(InsetCommandParams("bibitem"));
108 case LFUN_INSET_FLOAT:
109 // check if the float type exists
110 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
111 return new InsetFloat(params, cmd.argument);
112 lyxerr << "Non-existent float type: " << cmd.argument << endl;
115 case LFUN_INSET_WIDE_FLOAT:
116 // check if the float type exists
117 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
118 InsetFloat * p = new InsetFloat(params, cmd.argument);
119 p->wide(true, params);
121 lyxerr << "Non-existent float type: " << cmd.argument << endl;
124 case LFUN_INSET_WRAP:
125 if (cmd.argument == "figure")
126 return new InsetWrap(params, cmd.argument);
127 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
130 case LFUN_INDEX_INSERT: {
131 // Try and generate a valid index entry.
132 InsetCommandParams icp("index");
133 string const contents = cmd.argument.empty() ?
134 bv->getLyXText()->getStringToIndex() :
136 icp.setContents(contents);
138 string data = InsetCommandMailer::params2string("index", icp);
139 LyXView * lv = bv->owner();
141 if (icp.getContents().empty()) {
142 lv->getDialogs().show("index", data, 0);
144 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
149 case LFUN_TABULAR_INSERT:
150 if (!cmd.argument.empty()) {
151 std::istringstream ss(cmd.argument);
156 return new InsetTabular(*bv->buffer(), r, c);
158 bv->owner()->getDialogs().show("tabularcreate");
161 case LFUN_INSET_CAPTION:
162 if (bv->theLockingInset()) {
163 lyxerr << "Locking inset code: "
164 << static_cast<int>(bv->theLockingInset()->lyxCode());
165 InsetCaption * inset = new InsetCaption(params);
166 inset->setOwner(bv->theLockingInset());
167 inset->setAutoBreakRows(true);
168 inset->setDrawFrame(InsetText::LOCKED);
169 inset->setFrameColor(LColor::captionframe);
174 case LFUN_INDEX_PRINT:
175 return new InsetPrintIndex(InsetCommandParams("printindex"));
177 case LFUN_TOC_INSERT:
178 return new InsetTOC(InsetCommandParams("tableofcontents"));
180 case LFUN_ENVIRONMENT_INSERT:
181 return new InsetEnvironment(params, cmd.argument);
184 case LFUN_INSET_LIST:
185 return new InsetList;
187 case LFUN_INSET_THEOREM:
188 return new InsetTheorem;
191 case LFUN_INSET_INSERT: {
192 string const name = cmd.getArg(0);
194 if (name == "bibitem") {
195 InsetCommandParams icp;
196 InsetCommandMailer::string2params(cmd.argument, icp);
197 return new InsetBibitem(icp);
199 } else if (name == "bibtex") {
200 InsetCommandParams icp;
201 InsetCommandMailer::string2params(cmd.argument, icp);
202 return new InsetBibtex(icp);
204 } else if (name == "citation") {
205 InsetCommandParams icp;
206 InsetCommandMailer::string2params(cmd.argument, icp);
207 InsetCitation * inset = new InsetCitation(icp);
208 inset->setLoadingBuffer(*bv->buffer(), false);
211 } else if (name == "ert") {
212 InsetERT * inset = new InsetERT(params);
213 InsetERT::ERTStatus s;
214 InsetERTMailer::string2params(cmd.argument, s);
215 inset->status(bv, s);
218 } else if (name == "external") {
219 Buffer const & buffer = *cmd.view()->buffer();
220 InsetExternal::Params iep;
221 InsetExternalMailer::string2params(cmd.argument,
223 InsetExternal * inset = new InsetExternal;
224 inset->setParams(iep, buffer);
227 } else if (name == "graphics") {
228 Buffer const & buffer = *cmd.view()->buffer();
229 InsetGraphicsParams igp;
230 InsetGraphicsMailer::string2params(cmd.argument,
232 InsetGraphics * inset = new InsetGraphics;
233 inset->setParams(igp);
236 } else if (name == "include") {
237 InsetCommandParams iip;
238 InsetIncludeMailer::string2params(cmd.argument, iip);
239 return new InsetInclude(iip);
241 } else if (name == "index") {
242 InsetCommandParams icp;
243 InsetCommandMailer::string2params(cmd.argument, icp);
244 return new InsetIndex(icp);
246 } else if (name == "label") {
247 InsetCommandParams icp;
248 InsetCommandMailer::string2params(cmd.argument, icp);
249 return new InsetLabel(icp);
251 } else if (name == "ref") {
252 InsetCommandParams icp;
253 InsetCommandMailer::string2params(cmd.argument, icp);
254 return new InsetRef(icp, *bv->buffer());
256 } else if (name == "toc") {
257 InsetCommandParams icp;
258 InsetCommandMailer::string2params(cmd.argument, icp);
259 return new InsetTOC(icp);
261 } else if (name == "url") {
262 InsetCommandParams icp;
263 InsetCommandMailer::string2params(cmd.argument, icp);
264 return new InsetUrl(icp);
268 case LFUN_SPACE_INSERT: {
269 string const name = cmd.argument;
270 if (name == "normal")
271 return new InsetSpace(InsetSpace::NORMAL);
272 else if (name == "protected")
273 return new InsetSpace(InsetSpace::PROTECTED);
274 else if (name == "thin")
275 return new InsetSpace(InsetSpace::THIN);
276 else if (name == "quad")
277 return new InsetSpace(InsetSpace::QUAD);
278 else if (name == "qquad")
279 return new InsetSpace(InsetSpace::QQUAD);
280 else if (name == "enspace")
281 return new InsetSpace(InsetSpace::ENSPACE);
282 else if (name == "enskip")
283 return new InsetSpace(InsetSpace::ENSKIP);
284 else if (name == "negthinspace")
285 return new InsetSpace(InsetSpace::NEGTHIN);
286 else if (name.empty())
287 lyxerr << "LyX function 'space' needs an argument." << endl;
289 lyxerr << "Wrong argument for LyX function 'space'." << endl;
302 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
305 if (lex.getString() != "\\begin_inset") {
306 lyxerr << "Buffer::readInset: Consistency check failed."
310 InsetOld * inset = 0;
313 string const tmptok = lex.getString();
315 // test the different insets
316 if (tmptok == "LatexCommand") {
317 InsetCommandParams inscmd;
320 string const cmdName = inscmd.getCmdName();
322 // This strange command allows LyX to recognize "natbib" style
323 // citations: citet, citep, Citet etc.
324 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
325 inset = new InsetCitation(inscmd);
326 } else if (cmdName == "bibitem") {
327 lex.printError("Wrong place for bibitem");
328 inset = new InsetBibitem(inscmd);
329 } else if (cmdName == "bibtex") {
330 inset = new InsetBibtex(inscmd);
331 } else if (cmdName == "index") {
332 inset = new InsetIndex(inscmd);
333 } else if (cmdName == "include") {
334 inset = new InsetInclude(inscmd);
335 } else if (cmdName == "label") {
336 inset = new InsetLabel(inscmd);
337 } else if (cmdName == "url"
338 || cmdName == "htmlurl") {
339 inset = new InsetUrl(inscmd);
340 } else if (cmdName == "ref"
341 || cmdName == "eqref"
342 || cmdName == "pageref"
344 || cmdName == "vpageref"
345 || cmdName == "prettyref") {
346 if (!inscmd.getOptions().empty()
347 || !inscmd.getContents().empty()) {
348 inset = new InsetRef(inscmd, buf);
350 } else if (cmdName == "tableofcontents") {
351 inset = new InsetTOC(inscmd);
352 } else if (cmdName == "listofalgorithms") {
353 inset = new InsetFloatList("algorithm");
354 } else if (cmdName == "listoffigures") {
355 inset = new InsetFloatList("figure");
356 } else if (cmdName == "listoftables") {
357 inset = new InsetFloatList("table");
358 } else if (cmdName == "printindex") {
359 inset = new InsetPrintIndex(inscmd);
361 lyxerr << "unknown CommandInset '" << cmdName << "'" << std::endl;
365 if (tmptok == "Quotes") {
366 inset = new InsetQuotes;
367 } else if (tmptok == "External") {
368 inset = new InsetExternal;
369 } else if (tmptok == "FormulaMacro") {
370 inset = new InsetFormulaMacro;
371 } else if (tmptok == "Formula") {
372 inset = new InsetFormula;
373 } else if (tmptok == "Graphics") {
374 inset = new InsetGraphics;
375 } else if (tmptok == "Note" || tmptok == "Comment"
376 || tmptok == "Greyedout") {
377 inset = new InsetNote(buf.params(), tmptok);
378 } else if (tmptok == "Branch") {
379 inset = new InsetBranch(buf.params(), string());
380 } else if (tmptok == "Include") {
381 InsetCommandParams p("Include");
382 inset = new InsetInclude(p);
383 } else if (tmptok == "Environment") {
385 inset = new InsetEnvironment(buf.params(), lex.getString());
386 } else if (tmptok == "ERT") {
387 inset = new InsetERT(buf.params());
388 } else if (tmptok == "InsetSpace") {
389 inset = new InsetSpace;
390 } else if (tmptok == "Tabular") {
391 inset = new InsetTabular(buf);
392 } else if (tmptok == "Text") {
393 inset = new InsetText(buf.params());
394 } else if (tmptok == "Foot") {
395 inset = new InsetFoot(buf.params());
396 } else if (tmptok == "Marginal") {
397 inset = new InsetMarginal(buf.params());
398 } else if (tmptok == "OptArg") {
399 inset = new InsetOptArg(buf.params());
400 } else if (tmptok == "Minipage") {
401 inset = new InsetMinipage(buf.params());
402 } else if (tmptok == "Float") {
404 string tmptok = lex.getString();
405 inset = new InsetFloat(buf.params(), tmptok);
406 } else if (tmptok == "Wrap") {
408 string tmptok = lex.getString();
409 inset = new InsetWrap(buf.params(), tmptok);
411 } else if (tmptok == "List") {
412 inset = new InsetList;
413 } else if (tmptok == "Theorem") {
414 inset = new InsetList;
416 } else if (tmptok == "Caption") {
417 inset = new InsetCaption(buf.params());
418 } else if (tmptok == "FloatList") {
419 inset = new InsetFloatList;
421 lyxerr << "unknown Inset type '" << tmptok << "'" << std::endl;
425 inset->read(buf, lex);