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 using lyx::support::compare_ascii_no_case;
63 InsetOld * createInset(FuncRequest const & cmd)
65 BufferView * bv = cmd.view();
66 BufferParams const & params = bv->buffer()->params();
70 return new InsetHFill;
72 case LFUN_INSET_MINIPAGE:
73 return new InsetMinipage(params);
75 case LFUN_INSERT_NOTE:
77 string arg = cmd.getArg(0);
80 return new InsetNote(params, arg);
82 case LFUN_INSERT_BRANCH:
84 string arg = cmd.getArg(0);
87 return new InsetBranch(params, arg);
91 return new InsetERT(params);
93 case LFUN_INSET_FOOTNOTE:
94 return new InsetFoot(params);
96 case LFUN_INSET_MARGINAL:
97 return new InsetMarginal(params);
99 case LFUN_INSET_OPTARG:
100 return new InsetOptArg(params);
102 case LFUN_INSERT_BIBITEM:
103 return new InsetBibitem(InsetCommandParams("bibitem"));
105 case LFUN_INSET_FLOAT:
106 // check if the float type exists
107 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
108 return new InsetFloat(params, cmd.argument);
109 lyxerr << "Non-existent float type: " << cmd.argument << endl;
112 case LFUN_INSET_WIDE_FLOAT:
113 // check if the float type exists
114 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
115 InsetFloat * p = new InsetFloat(params, cmd.argument);
116 p->wide(true, params);
118 lyxerr << "Non-existent float type: " << cmd.argument << endl;
121 case LFUN_INSET_WRAP:
122 if (cmd.argument == "figure")
123 return new InsetWrap(params, cmd.argument);
124 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
127 case LFUN_INDEX_INSERT: {
128 // Try and generate a valid index entry.
129 InsetCommandParams icp("index");
130 string const contents = cmd.argument.empty() ?
131 bv->getLyXText()->getStringToIndex() :
133 icp.setContents(contents);
135 string data = InsetCommandMailer::params2string("index", icp);
136 LyXView * lv = bv->owner();
138 if (icp.getContents().empty()) {
139 lv->getDialogs().show("index", data, 0);
141 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
146 case LFUN_TABULAR_INSERT:
147 if (!cmd.argument.empty()) {
148 std::istringstream ss(cmd.argument);
153 return new InsetTabular(*bv->buffer(), r, c);
155 bv->owner()->getDialogs().show("tabularcreate");
158 case LFUN_INSET_CAPTION:
159 if (bv->theLockingInset()) {
160 lyxerr << "Locking inset code: "
161 << static_cast<int>(bv->theLockingInset()->lyxCode());
162 InsetCaption * inset = new InsetCaption(params);
163 inset->setOwner(bv->theLockingInset());
164 inset->setAutoBreakRows(true);
165 inset->setDrawFrame(InsetText::LOCKED);
166 inset->setFrameColor(LColor::captionframe);
171 case LFUN_INDEX_PRINT:
172 return new InsetPrintIndex(InsetCommandParams("printindex"));
174 case LFUN_TOC_INSERT:
175 return new InsetTOC(InsetCommandParams("tableofcontents"));
177 case LFUN_ENVIRONMENT_INSERT:
178 return new InsetEnvironment(params, cmd.argument);
181 case LFUN_INSET_LIST:
182 return new InsetList;
184 case LFUN_INSET_THEOREM:
185 return new InsetTheorem;
188 case LFUN_INSET_INSERT: {
189 string const name = cmd.getArg(0);
191 if (name == "bibitem") {
192 InsetCommandParams icp;
193 InsetCommandMailer::string2params(cmd.argument, icp);
194 return new InsetBibitem(icp);
196 } else if (name == "bibtex") {
197 InsetCommandParams icp;
198 InsetCommandMailer::string2params(cmd.argument, icp);
199 return new InsetBibtex(icp);
201 } else if (name == "citation") {
202 InsetCommandParams icp;
203 InsetCommandMailer::string2params(cmd.argument, icp);
204 InsetCitation * inset = new InsetCitation(icp);
205 inset->setLoadingBuffer(*bv->buffer(), false);
208 } else if (name == "ert") {
209 InsetERT * inset = new InsetERT(params);
210 InsetERT::ERTStatus s;
211 InsetERTMailer::string2params(cmd.argument, s);
212 inset->status(bv, s);
215 } else if (name == "external") {
216 Buffer const & buffer = *cmd.view()->buffer();
217 InsetExternal::Params iep;
218 InsetExternalMailer::string2params(cmd.argument,
220 InsetExternal * inset = new InsetExternal;
221 inset->setParams(iep, buffer);
224 } else if (name == "graphics") {
225 Buffer const & buffer = *cmd.view()->buffer();
226 InsetGraphicsParams igp;
227 InsetGraphicsMailer::string2params(cmd.argument,
229 InsetGraphics * inset = new InsetGraphics;
230 inset->setParams(igp);
233 } else if (name == "include") {
234 InsetCommandParams iip;
235 InsetIncludeMailer::string2params(cmd.argument, iip);
236 return new InsetInclude(iip);
238 } else if (name == "index") {
239 InsetCommandParams icp;
240 InsetCommandMailer::string2params(cmd.argument, icp);
241 return new InsetIndex(icp);
243 } else if (name == "label") {
244 InsetCommandParams icp;
245 InsetCommandMailer::string2params(cmd.argument, icp);
246 return new InsetLabel(icp);
248 } else if (name == "ref") {
249 InsetCommandParams icp;
250 InsetCommandMailer::string2params(cmd.argument, icp);
251 return new InsetRef(icp, *bv->buffer());
253 } else if (name == "toc") {
254 InsetCommandParams icp;
255 InsetCommandMailer::string2params(cmd.argument, icp);
256 return new InsetTOC(icp);
258 } else if (name == "url") {
259 InsetCommandParams icp;
260 InsetCommandMailer::string2params(cmd.argument, icp);
261 return new InsetUrl(icp);
265 case LFUN_SPACE_INSERT: {
266 string const name = cmd.argument;
267 if (name == "normal")
268 return new InsetSpace(InsetSpace::NORMAL);
269 else if (name == "protected")
270 return new InsetSpace(InsetSpace::PROTECTED);
271 else if (name == "thin")
272 return new InsetSpace(InsetSpace::THIN);
273 else if (name == "quad")
274 return new InsetSpace(InsetSpace::QUAD);
275 else if (name == "qquad")
276 return new InsetSpace(InsetSpace::QQUAD);
277 else if (name == "enspace")
278 return new InsetSpace(InsetSpace::ENSPACE);
279 else if (name == "enskip")
280 return new InsetSpace(InsetSpace::ENSKIP);
281 else if (name == "negthinspace")
282 return new InsetSpace(InsetSpace::NEGTHIN);
283 else if (name.empty())
284 lyxerr << "LyX function 'space' needs an argument." << endl;
286 lyxerr << "Wrong argument for LyX function 'space'." << endl;
299 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
302 if (lex.getString() != "\\begin_inset") {
303 lyxerr << "Buffer::readInset: Consistency check failed."
307 InsetOld * inset = 0;
310 string const tmptok = lex.getString();
312 // test the different insets
313 if (tmptok == "LatexCommand") {
314 InsetCommandParams inscmd;
317 string const cmdName = inscmd.getCmdName();
319 // This strange command allows LyX to recognize "natbib" style
320 // citations: citet, citep, Citet etc.
321 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
322 inset = new InsetCitation(inscmd);
323 } else if (cmdName == "bibitem") {
324 lex.printError("Wrong place for bibitem");
325 inset = new InsetBibitem(inscmd);
326 } else if (cmdName == "bibtex") {
327 inset = new InsetBibtex(inscmd);
328 } else if (cmdName == "index") {
329 inset = new InsetIndex(inscmd);
330 } else if (cmdName == "include") {
331 inset = new InsetInclude(inscmd);
332 } else if (cmdName == "label") {
333 inset = new InsetLabel(inscmd);
334 } else if (cmdName == "url"
335 || cmdName == "htmlurl") {
336 inset = new InsetUrl(inscmd);
337 } else if (cmdName == "ref"
338 || cmdName == "eqref"
339 || cmdName == "pageref"
341 || cmdName == "vpageref"
342 || cmdName == "prettyref") {
343 if (!inscmd.getOptions().empty()
344 || !inscmd.getContents().empty()) {
345 inset = new InsetRef(inscmd, buf);
347 } else if (cmdName == "tableofcontents") {
348 inset = new InsetTOC(inscmd);
349 } else if (cmdName == "listofalgorithms") {
350 inset = new InsetFloatList("algorithm");
351 } else if (cmdName == "listoffigures") {
352 inset = new InsetFloatList("figure");
353 } else if (cmdName == "listoftables") {
354 inset = new InsetFloatList("table");
355 } else if (cmdName == "printindex") {
356 inset = new InsetPrintIndex(inscmd);
359 if (tmptok == "Quotes") {
360 inset = new InsetQuotes;
361 } else if (tmptok == "External") {
362 inset = new InsetExternal;
363 } else if (tmptok == "FormulaMacro") {
364 inset = new InsetFormulaMacro;
365 } else if (tmptok == "Formula") {
366 inset = new InsetFormula;
367 } else if (tmptok == "Graphics") {
368 inset = new InsetGraphics;
369 } else if (tmptok == "Note" || tmptok == "Comment"
370 || tmptok == "Greyedout") {
371 inset = new InsetNote(buf.params(), tmptok);
372 } else if (tmptok == "Branch") {
373 inset = new InsetBranch(buf.params(), tmptok);
374 } else if (tmptok == "Include") {
375 InsetCommandParams p("Include");
376 inset = new InsetInclude(p);
377 } else if (tmptok == "Environment") {
379 inset = new InsetEnvironment(buf.params(), lex.getString());
380 } else if (tmptok == "ERT") {
381 inset = new InsetERT(buf.params());
382 } else if (tmptok == "InsetSpace") {
383 inset = new InsetSpace;
384 } else if (tmptok == "Tabular") {
385 inset = new InsetTabular(buf);
386 } else if (tmptok == "Text") {
387 inset = new InsetText(buf.params());
388 } else if (tmptok == "Foot") {
389 inset = new InsetFoot(buf.params());
390 } else if (tmptok == "Marginal") {
391 inset = new InsetMarginal(buf.params());
392 } else if (tmptok == "OptArg") {
393 inset = new InsetOptArg(buf.params());
394 } else if (tmptok == "Minipage") {
395 inset = new InsetMinipage(buf.params());
396 } else if (tmptok == "Float") {
398 string tmptok = lex.getString();
399 inset = new InsetFloat(buf.params(), tmptok);
400 } else if (tmptok == "Wrap") {
402 string tmptok = lex.getString();
403 inset = new InsetWrap(buf.params(), tmptok);
405 } else if (tmptok == "List") {
406 inset = new InsetList;
407 } else if (tmptok == "Theorem") {
408 inset = new InsetList;
410 } else if (tmptok == "Caption") {
411 inset = new InsetCaption(buf.params());
412 } else if (tmptok == "FloatList") {
413 inset = new InsetFloatList;
417 inset->read(buf, lex);