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;
65 InsetOld * createInset(FuncRequest const & cmd)
67 BufferView * bv = cmd.view();
68 BufferParams const & params = bv->buffer()->params();
72 return new InsetHFill;
74 case LFUN_INSET_MINIPAGE:
75 return new InsetMinipage(params);
77 case LFUN_INSERT_NOTE:
79 string arg = cmd.getArg(0);
82 return new InsetNote(params, arg);
84 case LFUN_INSERT_BRANCH:
86 string arg = cmd.getArg(0);
89 return new InsetBranch(params, arg);
93 return new InsetERT(params);
95 case LFUN_INSET_FOOTNOTE:
96 return new InsetFoot(params);
98 case LFUN_INSET_MARGINAL:
99 return new InsetMarginal(params);
101 case LFUN_INSET_OPTARG:
102 return new InsetOptArg(params);
104 case LFUN_INSERT_BIBITEM:
105 return new InsetBibitem(InsetCommandParams("bibitem"));
107 case LFUN_INSET_FLOAT:
108 // check if the float type exists
109 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
110 return new InsetFloat(params, cmd.argument);
111 lyxerr << "Non-existent float type: " << cmd.argument << endl;
114 case LFUN_INSET_WIDE_FLOAT:
115 // check if the float type exists
116 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
117 InsetFloat * p = new InsetFloat(params, cmd.argument);
118 p->wide(true, params);
120 lyxerr << "Non-existent float type: " << cmd.argument << endl;
123 case LFUN_INSET_WRAP:
124 if (cmd.argument == "figure")
125 return new InsetWrap(params, cmd.argument);
126 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
129 case LFUN_INDEX_INSERT: {
130 // Try and generate a valid index entry.
131 InsetCommandParams icp("index");
132 string const contents = cmd.argument.empty() ?
133 bv->getLyXText()->getStringToIndex() :
135 icp.setContents(contents);
137 string data = InsetCommandMailer::params2string("index", icp);
138 LyXView * lv = bv->owner();
140 if (icp.getContents().empty()) {
141 lv->getDialogs().show("index", data, 0);
143 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
148 case LFUN_TABULAR_INSERT:
149 if (!cmd.argument.empty()) {
150 std::istringstream ss(cmd.argument);
155 return new InsetTabular(*bv->buffer(), r, c);
157 bv->owner()->getDialogs().show("tabularcreate");
160 case LFUN_INSET_CAPTION:
161 if (bv->theLockingInset()) {
162 lyxerr << "Locking inset code: "
163 << static_cast<int>(bv->theLockingInset()->lyxCode());
164 InsetCaption * inset = new InsetCaption(params);
165 inset->setOwner(bv->theLockingInset());
166 inset->setAutoBreakRows(true);
167 inset->setDrawFrame(InsetText::LOCKED);
168 inset->setFrameColor(LColor::captionframe);
173 case LFUN_INDEX_PRINT:
174 return new InsetPrintIndex(InsetCommandParams("printindex"));
176 case LFUN_TOC_INSERT:
177 return new InsetTOC(InsetCommandParams("tableofcontents"));
179 case LFUN_ENVIRONMENT_INSERT:
180 return new InsetEnvironment(params, cmd.argument);
183 case LFUN_INSET_LIST:
184 return new InsetList;
186 case LFUN_INSET_THEOREM:
187 return new InsetTheorem;
190 case LFUN_INSET_INSERT: {
191 string const name = cmd.getArg(0);
193 if (name == "bibitem") {
194 InsetCommandParams icp;
195 InsetCommandMailer::string2params(cmd.argument, icp);
196 return new InsetBibitem(icp);
198 } else if (name == "bibtex") {
199 InsetCommandParams icp;
200 InsetCommandMailer::string2params(cmd.argument, icp);
201 return new InsetBibtex(icp);
203 } else if (name == "citation") {
204 InsetCommandParams icp;
205 InsetCommandMailer::string2params(cmd.argument, icp);
206 InsetCitation * inset = new InsetCitation(icp);
207 inset->setLoadingBuffer(*bv->buffer(), false);
210 } else if (name == "ert") {
211 InsetERT * inset = new InsetERT(params);
212 InsetERT::ERTStatus s;
213 InsetERTMailer::string2params(cmd.argument, s);
214 inset->status(bv, s);
217 } else if (name == "external") {
218 Buffer const & buffer = *cmd.view()->buffer();
219 InsetExternal::Params iep;
220 InsetExternalMailer::string2params(cmd.argument,
222 InsetExternal * inset = new InsetExternal;
223 inset->setParams(iep, buffer);
226 } else if (name == "graphics") {
227 Buffer const & buffer = *cmd.view()->buffer();
228 InsetGraphicsParams igp;
229 InsetGraphicsMailer::string2params(cmd.argument,
231 InsetGraphics * inset = new InsetGraphics;
232 inset->setParams(igp);
235 } else if (name == "include") {
236 InsetCommandParams iip;
237 InsetIncludeMailer::string2params(cmd.argument, iip);
238 return new InsetInclude(iip);
240 } else if (name == "index") {
241 InsetCommandParams icp;
242 InsetCommandMailer::string2params(cmd.argument, icp);
243 return new InsetIndex(icp);
245 } else if (name == "label") {
246 InsetCommandParams icp;
247 InsetCommandMailer::string2params(cmd.argument, icp);
248 return new InsetLabel(icp);
250 } else if (name == "ref") {
251 InsetCommandParams icp;
252 InsetCommandMailer::string2params(cmd.argument, icp);
253 return new InsetRef(icp, *bv->buffer());
255 } else if (name == "toc") {
256 InsetCommandParams icp;
257 InsetCommandMailer::string2params(cmd.argument, icp);
258 return new InsetTOC(icp);
260 } else if (name == "url") {
261 InsetCommandParams icp;
262 InsetCommandMailer::string2params(cmd.argument, icp);
263 return new InsetUrl(icp);
267 case LFUN_SPACE_INSERT: {
268 string const name = cmd.argument;
269 if (name == "normal")
270 return new InsetSpace(InsetSpace::NORMAL);
271 else if (name == "protected")
272 return new InsetSpace(InsetSpace::PROTECTED);
273 else if (name == "thin")
274 return new InsetSpace(InsetSpace::THIN);
275 else if (name == "quad")
276 return new InsetSpace(InsetSpace::QUAD);
277 else if (name == "qquad")
278 return new InsetSpace(InsetSpace::QQUAD);
279 else if (name == "enspace")
280 return new InsetSpace(InsetSpace::ENSPACE);
281 else if (name == "enskip")
282 return new InsetSpace(InsetSpace::ENSKIP);
283 else if (name == "negthinspace")
284 return new InsetSpace(InsetSpace::NEGTHIN);
285 else if (name.empty())
286 lyxerr << "LyX function 'space' needs an argument." << endl;
288 lyxerr << "Wrong argument for LyX function 'space'." << endl;
301 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
304 if (lex.getString() != "\\begin_inset") {
305 lyxerr << "Buffer::readInset: Consistency check failed."
309 InsetOld * inset = 0;
312 string const tmptok = lex.getString();
314 // test the different insets
315 if (tmptok == "LatexCommand") {
316 InsetCommandParams inscmd;
319 string const cmdName = inscmd.getCmdName();
321 // This strange command allows LyX to recognize "natbib" style
322 // citations: citet, citep, Citet etc.
323 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
324 inset = new InsetCitation(inscmd);
325 } else if (cmdName == "bibitem") {
326 lex.printError("Wrong place for bibitem");
327 inset = new InsetBibitem(inscmd);
328 } else if (cmdName == "bibtex") {
329 inset = new InsetBibtex(inscmd);
330 } else if (cmdName == "index") {
331 inset = new InsetIndex(inscmd);
332 } else if (cmdName == "include") {
333 inset = new InsetInclude(inscmd);
334 } else if (cmdName == "label") {
335 inset = new InsetLabel(inscmd);
336 } else if (cmdName == "url"
337 || cmdName == "htmlurl") {
338 inset = new InsetUrl(inscmd);
339 } else if (cmdName == "ref"
340 || cmdName == "eqref"
341 || cmdName == "pageref"
343 || cmdName == "vpageref"
344 || cmdName == "prettyref") {
345 if (!inscmd.getOptions().empty()
346 || !inscmd.getContents().empty()) {
347 inset = new InsetRef(inscmd, buf);
349 } else if (cmdName == "tableofcontents") {
350 inset = new InsetTOC(inscmd);
351 } else if (cmdName == "listofalgorithms") {
352 inset = new InsetFloatList("algorithm");
353 } else if (cmdName == "listoffigures") {
354 inset = new InsetFloatList("figure");
355 } else if (cmdName == "listoftables") {
356 inset = new InsetFloatList("table");
357 } else if (cmdName == "printindex") {
358 inset = new InsetPrintIndex(inscmd);
360 lyxerr << "unknown CommandInset '" << cmdName << "'" << std::endl;
364 if (tmptok == "Quotes") {
365 inset = new InsetQuotes;
366 } else if (tmptok == "External") {
367 inset = new InsetExternal;
368 } else if (tmptok == "FormulaMacro") {
369 inset = new InsetFormulaMacro;
370 } else if (tmptok == "Formula") {
371 inset = new InsetFormula;
372 } else if (tmptok == "Graphics") {
373 inset = new InsetGraphics;
374 } else if (tmptok == "Note" || tmptok == "Comment"
375 || tmptok == "Greyedout") {
376 inset = new InsetNote(buf.params(), tmptok);
377 } else if (tmptok == "Branch") {
378 inset = new InsetBranch(buf.params(), string());
379 } else if (tmptok == "Include") {
380 InsetCommandParams p("Include");
381 inset = new InsetInclude(p);
382 } else if (tmptok == "Environment") {
384 inset = new InsetEnvironment(buf.params(), lex.getString());
385 } else if (tmptok == "ERT") {
386 inset = new InsetERT(buf.params());
387 } else if (tmptok == "InsetSpace") {
388 inset = new InsetSpace;
389 } else if (tmptok == "Tabular") {
390 inset = new InsetTabular(buf);
391 } else if (tmptok == "Text") {
392 inset = new InsetText(buf.params());
393 } else if (tmptok == "Foot") {
394 inset = new InsetFoot(buf.params());
395 } else if (tmptok == "Marginal") {
396 inset = new InsetMarginal(buf.params());
397 } else if (tmptok == "OptArg") {
398 inset = new InsetOptArg(buf.params());
399 } else if (tmptok == "Minipage") {
400 inset = new InsetMinipage(buf.params());
401 } else if (tmptok == "Float") {
403 string tmptok = lex.getString();
404 inset = new InsetFloat(buf.params(), tmptok);
405 } else if (tmptok == "Wrap") {
407 string tmptok = lex.getString();
408 inset = new InsetWrap(buf.params(), tmptok);
410 } else if (tmptok == "List") {
411 inset = new InsetList;
412 } else if (tmptok == "Theorem") {
413 inset = new InsetList;
415 } else if (tmptok == "Caption") {
416 inset = new InsetCaption(buf.params());
417 } else if (tmptok == "FloatList") {
418 inset = new InsetFloatList;
420 lyxerr << "unknown Inset type '" << tmptok << "'" << std::endl;
424 inset->read(buf, lex);