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/insetbox.h"
43 #include "insets/insetbranch.h"
44 #include "insets/insetoptarg.h"
45 #include "insets/insetref.h"
46 #include "insets/insetspace.h"
47 #include "insets/insettabular.h"
48 #include "insets/insettoc.h"
49 #include "insets/inseturl.h"
50 #include "insets/insetwrap.h"
51 #include "mathed/formulamacro.h"
52 #include "mathed/formula.h"
54 #include "frontends/Dialogs.h"
55 #include "frontends/LyXView.h"
56 #include "support/lstrings.h"
57 #include "support/std_sstream.h"
59 #include <boost/assert.hpp>
61 using lyx::support::compare_ascii_no_case;
67 InsetOld * createInset(FuncRequest const & cmd)
69 BufferView * bv = cmd.view();
70 BufferParams const & params = bv->buffer()->params();
74 return new InsetHFill;
76 case LFUN_INSET_MINIPAGE:
77 return new InsetMinipage(params);
79 case LFUN_INSERT_NOTE:
81 string arg = cmd.getArg(0);
84 return new InsetNote(params, arg);
88 string arg = cmd.getArg(0);
91 return new InsetBox(params, arg);
93 case LFUN_INSERT_BRANCH:
95 string arg = cmd.getArg(0);
98 return new InsetBranch(params, arg);
102 return new InsetERT(params);
104 case LFUN_INSET_FOOTNOTE:
105 return new InsetFoot(params);
107 case LFUN_INSET_MARGINAL:
108 return new InsetMarginal(params);
110 case LFUN_INSET_OPTARG:
111 return new InsetOptArg(params);
113 case LFUN_INSERT_BIBITEM:
114 return new InsetBibitem(InsetCommandParams("bibitem"));
116 case LFUN_INSET_FLOAT:
117 // check if the float type exists
118 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
119 return new InsetFloat(params, cmd.argument);
120 lyxerr << "Non-existent float type: " << cmd.argument << endl;
123 case LFUN_INSET_WIDE_FLOAT:
124 // check if the float type exists
125 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
126 InsetFloat * p = new InsetFloat(params, cmd.argument);
127 p->wide(true, params);
129 lyxerr << "Non-existent float type: " << cmd.argument << endl;
132 case LFUN_INSET_WRAP:
133 if (cmd.argument == "figure")
134 return new InsetWrap(params, cmd.argument);
135 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
138 case LFUN_INDEX_INSERT: {
139 // Try and generate a valid index entry.
140 InsetCommandParams icp("index");
141 string const contents = cmd.argument.empty() ?
142 bv->getLyXText()->getStringToIndex() :
144 icp.setContents(contents);
146 string data = InsetCommandMailer::params2string("index", icp);
147 LyXView * lv = bv->owner();
149 if (icp.getContents().empty()) {
150 lv->getDialogs().show("index", data, 0);
152 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
157 case LFUN_TABULAR_INSERT:
158 if (!cmd.argument.empty()) {
159 std::istringstream ss(cmd.argument);
164 return new InsetTabular(*bv->buffer(), r, c);
166 bv->owner()->getDialogs().show("tabularcreate");
169 case LFUN_INSET_CAPTION:
170 if (bv->theLockingInset()) {
171 lyxerr << "Locking inset code: "
172 << static_cast<int>(bv->theLockingInset()->lyxCode());
173 InsetCaption * inset = new InsetCaption(params);
174 inset->setOwner(bv->theLockingInset());
175 inset->setAutoBreakRows(true);
176 inset->setDrawFrame(InsetText::LOCKED);
177 inset->setFrameColor(LColor::captionframe);
182 case LFUN_INDEX_PRINT:
183 return new InsetPrintIndex(InsetCommandParams("printindex"));
185 case LFUN_TOC_INSERT:
186 return new InsetTOC(InsetCommandParams("tableofcontents"));
188 case LFUN_ENVIRONMENT_INSERT:
189 return new InsetEnvironment(params, cmd.argument);
192 case LFUN_INSET_LIST:
193 return new InsetList;
195 case LFUN_INSET_THEOREM:
196 return new InsetTheorem;
199 case LFUN_INSET_INSERT: {
200 string const name = cmd.getArg(0);
202 if (name == "bibitem") {
203 InsetCommandParams icp;
204 InsetCommandMailer::string2params(cmd.argument, icp);
205 return new InsetBibitem(icp);
207 } else if (name == "bibtex") {
208 InsetCommandParams icp;
209 InsetCommandMailer::string2params(cmd.argument, icp);
210 return new InsetBibtex(icp);
212 } else if (name == "citation") {
213 InsetCommandParams icp;
214 InsetCommandMailer::string2params(cmd.argument, icp);
215 InsetCitation * inset = new InsetCitation(icp);
216 inset->setLoadingBuffer(*bv->buffer(), false);
219 } else if (name == "ert") {
220 InsetERT * inset = new InsetERT(params);
221 InsetERT::ERTStatus s;
222 InsetERTMailer::string2params(cmd.argument, s);
223 inset->status(bv, s);
226 } else if (name == "external") {
227 Buffer const & buffer = *cmd.view()->buffer();
228 InsetExternalParams iep;
229 InsetExternalMailer::string2params(cmd.argument,
231 InsetExternal * inset = new InsetExternal;
232 inset->setParams(iep, buffer);
235 } else if (name == "graphics") {
236 Buffer const & buffer = *cmd.view()->buffer();
237 InsetGraphicsParams igp;
238 InsetGraphicsMailer::string2params(cmd.argument,
240 InsetGraphics * inset = new InsetGraphics;
241 inset->setParams(igp);
244 } else if (name == "include") {
245 InsetCommandParams iip;
246 InsetIncludeMailer::string2params(cmd.argument, iip);
247 return new InsetInclude(iip);
249 } else if (name == "index") {
250 InsetCommandParams icp;
251 InsetCommandMailer::string2params(cmd.argument, icp);
252 return new InsetIndex(icp);
254 } else if (name == "label") {
255 InsetCommandParams icp;
256 InsetCommandMailer::string2params(cmd.argument, icp);
257 return new InsetLabel(icp);
259 } else if (name == "ref") {
260 InsetCommandParams icp;
261 InsetCommandMailer::string2params(cmd.argument, icp);
262 return new InsetRef(icp, *bv->buffer());
264 } else if (name == "toc") {
265 InsetCommandParams icp;
266 InsetCommandMailer::string2params(cmd.argument, icp);
267 return new InsetTOC(icp);
269 } else if (name == "url") {
270 InsetCommandParams icp;
271 InsetCommandMailer::string2params(cmd.argument, icp);
272 return new InsetUrl(icp);
276 case LFUN_SPACE_INSERT: {
277 string const name = cmd.argument;
278 if (name == "normal")
279 return new InsetSpace(InsetSpace::NORMAL);
280 else if (name == "protected")
281 return new InsetSpace(InsetSpace::PROTECTED);
282 else if (name == "thin")
283 return new InsetSpace(InsetSpace::THIN);
284 else if (name == "quad")
285 return new InsetSpace(InsetSpace::QUAD);
286 else if (name == "qquad")
287 return new InsetSpace(InsetSpace::QQUAD);
288 else if (name == "enspace")
289 return new InsetSpace(InsetSpace::ENSPACE);
290 else if (name == "enskip")
291 return new InsetSpace(InsetSpace::ENSKIP);
292 else if (name == "negthinspace")
293 return new InsetSpace(InsetSpace::NEGTHIN);
294 else if (name.empty())
295 lyxerr << "LyX function 'space' needs an argument." << endl;
297 lyxerr << "Wrong argument for LyX function 'space'." << endl;
310 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
313 if (lex.getString() != "\\begin_inset") {
314 lyxerr << "Buffer::readInset: Consistency check failed."
318 InsetOld * inset = 0;
321 string const tmptok = lex.getString();
323 // test the different insets
324 if (tmptok == "LatexCommand") {
325 InsetCommandParams inscmd;
328 string const cmdName = inscmd.getCmdName();
330 // This strange command allows LyX to recognize "natbib" style
331 // citations: citet, citep, Citet etc.
332 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
333 inset = new InsetCitation(inscmd);
334 } else if (cmdName == "bibitem") {
335 lex.printError("Wrong place for bibitem");
336 inset = new InsetBibitem(inscmd);
337 } else if (cmdName == "bibtex") {
338 inset = new InsetBibtex(inscmd);
339 } else if (cmdName == "index") {
340 inset = new InsetIndex(inscmd);
341 } else if (cmdName == "include") {
342 inset = new InsetInclude(inscmd);
343 } else if (cmdName == "label") {
344 inset = new InsetLabel(inscmd);
345 } else if (cmdName == "url"
346 || cmdName == "htmlurl") {
347 inset = new InsetUrl(inscmd);
348 } else if (cmdName == "ref"
349 || cmdName == "eqref"
350 || cmdName == "pageref"
352 || cmdName == "vpageref"
353 || cmdName == "prettyref") {
354 if (!inscmd.getOptions().empty()
355 || !inscmd.getContents().empty()) {
356 inset = new InsetRef(inscmd, buf);
358 } else if (cmdName == "tableofcontents") {
359 inset = new InsetTOC(inscmd);
360 } else if (cmdName == "listofalgorithms") {
361 inset = new InsetFloatList("algorithm");
362 } else if (cmdName == "listoffigures") {
363 inset = new InsetFloatList("figure");
364 } else if (cmdName == "listoftables") {
365 inset = new InsetFloatList("table");
366 } else if (cmdName == "printindex") {
367 inset = new InsetPrintIndex(inscmd);
369 lyxerr << "unknown CommandInset '" << cmdName
371 while (lex.isOK() && lex.getString() != "\\end_inset")
376 if (tmptok == "Quotes") {
377 inset = new InsetQuotes;
378 } else if (tmptok == "External") {
379 inset = new InsetExternal;
380 } else if (tmptok == "FormulaMacro") {
381 inset = new InsetFormulaMacro;
382 } else if (tmptok == "Formula") {
383 inset = new InsetFormula;
384 } else if (tmptok == "Graphics") {
385 inset = new InsetGraphics;
386 } else if (tmptok == "Note" || tmptok == "Comment"
387 || tmptok == "Greyedout") {
388 inset = new InsetNote(buf.params(), tmptok);
389 } else if (tmptok == "Boxed" || tmptok == "ovalbox"
390 || tmptok == "Shadowbox" || tmptok == "Doublebox"
391 || tmptok == "Ovalbox" || tmptok == "Frameless") {
392 inset = new InsetBox(buf.params(), tmptok);
393 } else if (tmptok == "Branch") {
394 inset = new InsetBranch(buf.params(), string());
395 } else if (tmptok == "Include") {
396 InsetCommandParams p("Include");
397 inset = new InsetInclude(p);
398 } else if (tmptok == "Environment") {
400 inset = new InsetEnvironment(buf.params(), lex.getString());
401 } else if (tmptok == "ERT") {
402 inset = new InsetERT(buf.params());
403 } else if (tmptok == "InsetSpace") {
404 inset = new InsetSpace;
405 } else if (tmptok == "Tabular") {
406 inset = new InsetTabular(buf);
407 } else if (tmptok == "Text") {
408 inset = new InsetText(buf.params());
409 } else if (tmptok == "Foot") {
410 inset = new InsetFoot(buf.params());
411 } else if (tmptok == "Marginal") {
412 inset = new InsetMarginal(buf.params());
413 } else if (tmptok == "OptArg") {
414 inset = new InsetOptArg(buf.params());
415 } else if (tmptok == "Minipage") {
416 inset = new InsetMinipage(buf.params());
417 } else if (tmptok == "Float") {
419 string tmptok = lex.getString();
420 inset = new InsetFloat(buf.params(), tmptok);
421 } else if (tmptok == "Wrap") {
423 string tmptok = lex.getString();
424 inset = new InsetWrap(buf.params(), tmptok);
426 } else if (tmptok == "List") {
427 inset = new InsetList;
428 } else if (tmptok == "Theorem") {
429 inset = new InsetList;
431 } else if (tmptok == "Caption") {
432 inset = new InsetCaption(buf.params());
433 } else if (tmptok == "FloatList") {
434 inset = new InsetFloatList;
436 lyxerr << "unknown Inset type '" << tmptok
438 while (lex.isOK() && lex.getString() != "\\end_inset")
443 inset->read(buf, lex);