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
13 #include "funcrequest.h"
14 #include "bufferparams.h"
16 #include "FloatList.h"
18 #include "BufferView.h"
22 #include "insets/insetbibitem.h"
23 #include "insets/insetbibtex.h"
24 #include "insets/insetcaption.h"
25 #include "insets/insetcite.h"
26 #include "insets/insetenv.h"
27 #include "insets/insetert.h"
28 #include "insets/insetexternal.h"
29 #include "insets/insetfloat.h"
30 #include "insets/insetfloatlist.h"
31 #include "insets/insetfoot.h"
32 #include "insets/insetgraphics.h"
33 #include "insets/insethfill.h"
34 #include "insets/insetinclude.h"
35 #include "insets/insetindex.h"
36 #include "insets/insetlabel.h"
37 #include "insets/insetmarginal.h"
38 #include "insets/insetminipage.h"
39 #include "insets/insetnote.h"
40 #include "insets/insetoptarg.h"
41 #include "insets/insetref.h"
42 #include "insets/insetspace.h"
43 #include "insets/insettabular.h"
44 #include "insets/insettext.h"
45 #include "insets/insettoc.h"
46 #include "insets/inseturl.h"
47 #include "insets/insetwrap.h"
48 #include "mathed/formulamacro.h"
49 #include "mathed/formula.h"
51 #include "frontends/Dialogs.h"
52 #include "frontends/LyXView.h"
53 #include "support/lstrings.h"
59 Inset * createInset(FuncRequest const & cmd)
61 BufferView * bv = cmd.view();
62 BufferParams const & params = bv->buffer()->params;
66 return new InsetHFill();
68 case LFUN_INSET_MINIPAGE:
69 return new InsetMinipage(params);
71 case LFUN_INSERT_NOTE:
72 return new InsetNote(params);
75 return new InsetERT(params);
77 case LFUN_INSET_FOOTNOTE:
78 return new InsetFoot(params);
80 case LFUN_INSET_MARGINAL:
81 return new InsetMarginal(params);
83 case LFUN_INSET_OPTARG:
84 return new InsetOptArg(params);
86 case LFUN_INSERT_BIBITEM:
87 return new InsetBibitem(InsetCommandParams("bibitem"));
89 case LFUN_INSET_FLOAT:
90 // check if the float type exists
91 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
92 return new InsetFloat(params, cmd.argument);
93 lyxerr << "Non-existent float type: " << cmd.argument << endl;
96 case LFUN_INSET_WIDE_FLOAT:
97 // check if the float type exists
98 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
99 InsetFloat * p = new InsetFloat(params, cmd.argument);
100 p->wide(true, params);
102 lyxerr << "Non-existent float type: " << cmd.argument << endl;
105 case LFUN_INSET_WRAP:
106 if (cmd.argument == "figure")
107 return new InsetWrap(params, cmd.argument);
108 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
111 case LFUN_INDEX_INSERT: {
112 // Try and generate a valid index entry.
113 InsetCommandParams icp("index");
114 string const contents = cmd.argument.empty() ?
115 bv->getLyXText()->getStringToIndex() :
117 icp.setContents(contents);
119 string data = InsetCommandMailer::params2string("index", icp);
120 LyXView * lv = bv->owner();
122 if (icp.getContents().empty()) {
123 lv->getDialogs().show("index", data, 0);
125 FuncRequest fr(bv, LFUN_INSET_APPLY, data);
131 case LFUN_TABULAR_INSERT:
132 if (!cmd.argument.empty()) {
135 ::sscanf(cmd.argument.c_str(),"%d%d", &r, &c);
136 return new InsetTabular(*bv->buffer(), r, c);
138 bv->owner()->getDialogs().show("tabularcreate");
141 case LFUN_INSET_CAPTION:
142 if (bv->theLockingInset()) {
143 lyxerr << "Locking inset code: "
144 << static_cast<int>(bv->theLockingInset()->lyxCode());
145 InsetCaption * inset = new InsetCaption(params);
146 inset->setOwner(bv->theLockingInset());
147 inset->setAutoBreakRows(true);
148 inset->setDrawFrame(0, InsetText::LOCKED);
149 inset->setFrameColor(0, LColor::captionframe);
154 case LFUN_INDEX_PRINT:
155 return new InsetPrintIndex(InsetCommandParams("printindex"));
157 case LFUN_TOC_INSERT:
158 return new InsetTOC(InsetCommandParams("tableofcontents"));
160 case LFUN_ENVIRONMENT_INSERT:
161 return new InsetEnvironment(params, cmd.argument);
164 case LFUN_INSET_LIST:
165 return new InsetList;
167 case LFUN_INSET_THEOREM:
168 return new InsetTheorem;
171 case LFUN_INSET_INSERT: {
172 string const name = cmd.getArg(0);
174 if (name == "bibitem") {
175 InsetCommandParams icp;
176 InsetCommandMailer::string2params(cmd.argument, icp);
177 return new InsetBibitem(icp);
179 } else if (name == "bibtex") {
180 InsetCommandParams icp;
181 InsetCommandMailer::string2params(cmd.argument, icp);
182 return new InsetBibtex(icp);
184 } else if (name == "citation") {
185 InsetCommandParams icp;
186 InsetCommandMailer::string2params(cmd.argument, icp);
187 InsetCitation * inset = new InsetCitation(icp);
188 inset->setLoadingBuffer(bv->buffer(), false);
191 } else if (name == "ert") {
192 InsetERT * inset = new InsetERT(params);
193 InsetERT::ERTStatus s;
194 InsetERTMailer::string2params(cmd.argument, s);
195 inset->status(bv, s);
198 } else if (name == "external") {
199 InsetExternal::Params iep;
200 InsetExternalMailer::string2params(cmd.argument, iep);
201 InsetExternal * inset = new InsetExternal;
202 inset->setFromParams(iep);
205 } else if (name == "graphics") {
206 InsetGraphicsParams igp;
207 InsetGraphicsMailer::string2params(cmd.argument, igp);
208 InsetGraphics * inset = new InsetGraphics;
209 inset->setParams(igp);
212 } else if (name == "include") {
213 InsetInclude::Params iip;
214 InsetIncludeMailer::string2params(cmd.argument, iip);
215 return new InsetInclude(iip);
217 } else if (name == "index") {
218 InsetCommandParams icp;
219 InsetCommandMailer::string2params(cmd.argument, icp);
220 return new InsetIndex(icp);
222 } else if (name == "label") {
223 InsetCommandParams icp;
224 InsetCommandMailer::string2params(cmd.argument, icp);
225 return new InsetLabel(icp);
227 } else if (name == "ref") {
228 InsetCommandParams icp;
229 InsetCommandMailer::string2params(cmd.argument, icp);
230 return new InsetRef(icp, *bv->buffer());
232 } else if (name == "toc") {
233 InsetCommandParams icp;
234 InsetCommandMailer::string2params(cmd.argument, icp);
235 return new InsetTOC(icp);
237 } else if (name == "url") {
238 InsetCommandParams icp;
239 InsetCommandMailer::string2params(cmd.argument, icp);
240 return new InsetUrl(icp);
244 case LFUN_SPACE_INSERT: {
245 string const name = cmd.argument;
246 if (name == "normal")
247 return new InsetSpace(InsetSpace::NORMAL);
248 else if (name == "protected")
249 return new InsetSpace(InsetSpace::PROTECTED);
250 else if (name == "thin")
251 return new InsetSpace(InsetSpace::THIN);
252 else if (name == "quad")
253 return new InsetSpace(InsetSpace::QUAD);
254 else if (name == "qquad")
255 return new InsetSpace(InsetSpace::QQUAD);
256 else if (name == "enspace")
257 return new InsetSpace(InsetSpace::ENSPACE);
258 else if (name == "enskip")
259 return new InsetSpace(InsetSpace::ENSKIP);
260 else if (name == "negthinspace")
261 return new InsetSpace(InsetSpace::NEGTHIN);
262 else if (name.empty())
263 lyxerr << "LyX function 'space' needs an argument." << endl;
265 lyxerr << "Wrong argument for LyX function 'space'." << endl;
278 Inset * readInset(LyXLex & lex, Buffer const & buf)
281 if (lex.getString() != "\\begin_inset") {
282 lyxerr << "Buffer::readInset: Consistency check failed."
289 string const tmptok = lex.getString();
291 // test the different insets
292 if (tmptok == "LatexCommand") {
293 InsetCommandParams inscmd;
296 string const cmdName = inscmd.getCmdName();
298 // This strange command allows LyX to recognize "natbib" style
299 // citations: citet, citep, Citet etc.
300 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
301 inset = new InsetCitation(inscmd);
302 } else if (cmdName == "bibitem") {
303 lex.printError("Wrong place for bibitem");
304 inset = new InsetBibitem(inscmd);
305 } else if (cmdName == "bibtex") {
306 inset = new InsetBibtex(inscmd);
307 } else if (cmdName == "index") {
308 inset = new InsetIndex(inscmd);
309 } else if (cmdName == "include") {
310 inset = new InsetInclude(inscmd, buf);
311 } else if (cmdName == "label") {
312 inset = new InsetLabel(inscmd);
313 } else if (cmdName == "url"
314 || cmdName == "htmlurl") {
315 inset = new InsetUrl(inscmd);
316 } else if (cmdName == "ref"
317 || cmdName == "eqref"
318 || cmdName == "pageref"
320 || cmdName == "vpageref"
321 || cmdName == "prettyref") {
322 if (!inscmd.getOptions().empty()
323 || !inscmd.getContents().empty()) {
324 inset = new InsetRef(inscmd, buf);
326 } else if (cmdName == "tableofcontents") {
327 inset = new InsetTOC(inscmd);
328 } else if (cmdName == "listofalgorithms") {
329 inset = new InsetFloatList("algorithm");
330 } else if (cmdName == "listoffigures") {
331 inset = new InsetFloatList("figure");
332 } else if (cmdName == "listoftables") {
333 inset = new InsetFloatList("table");
334 } else if (cmdName == "printindex") {
335 inset = new InsetPrintIndex(inscmd);
338 if (tmptok == "Quotes") {
339 inset = new InsetQuotes;
340 } else if (tmptok == "External") {
341 inset = new InsetExternal;
342 } else if (tmptok == "FormulaMacro") {
343 inset = new InsetFormulaMacro;
344 } else if (tmptok == "Formula") {
345 inset = new InsetFormula;
346 } else if (tmptok == "Graphics") {
347 inset = new InsetGraphics;
348 } else if (tmptok == "Note") {
349 inset = new InsetNote(buf.params);
350 } else if (tmptok == "Include") {
351 InsetCommandParams p("Include");
352 inset = new InsetInclude(p, buf);
353 } else if (tmptok == "Environment") {
355 inset = new InsetEnvironment(buf.params, lex.getString());
356 } else if (tmptok == "ERT") {
357 inset = new InsetERT(buf.params);
358 } else if (tmptok == "InsetSpace") {
359 inset = new InsetSpace;
360 } else if (tmptok == "Tabular") {
361 inset = new InsetTabular(buf);
362 } else if (tmptok == "Text") {
363 inset = new InsetText(buf.params);
364 } else if (tmptok == "Foot") {
365 inset = new InsetFoot(buf.params);
366 } else if (tmptok == "Marginal") {
367 inset = new InsetMarginal(buf.params);
368 } else if (tmptok == "OptArg") {
369 inset = new InsetOptArg(buf.params);
370 } else if (tmptok == "Minipage") {
371 inset = new InsetMinipage(buf.params);
372 } else if (tmptok == "Float") {
374 string tmptok = lex.getString();
375 inset = new InsetFloat(buf.params, tmptok);
376 } else if (tmptok == "Wrap") {
378 string tmptok = lex.getString();
379 inset = new InsetWrap(buf.params, tmptok);
381 } else if (tmptok == "List") {
382 inset = new InsetList;
383 } else if (tmptok == "Theorem") {
384 inset = new InsetList;
386 } else if (tmptok == "Caption") {
387 inset = new InsetCaption(buf.params);
388 } else if (tmptok == "FloatList") {
389 inset = new InsetFloatList;
393 inset->read(&buf, lex);