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"
57 using namespace lyx::support;
61 InsetOld * createInset(FuncRequest const & cmd)
63 BufferView * bv = cmd.view();
64 BufferParams const & params = bv->buffer()->params;
68 return new InsetHFill();
70 case LFUN_INSET_MINIPAGE:
71 return new InsetMinipage(params);
73 case LFUN_INSERT_NOTE:
75 string arg = cmd.getArg(0);
78 return new InsetNote(params, arg);
81 return new InsetERT(params);
83 case LFUN_INSET_FOOTNOTE:
84 return new InsetFoot(params);
86 case LFUN_INSET_MARGINAL:
87 return new InsetMarginal(params);
89 case LFUN_INSET_OPTARG:
90 return new InsetOptArg(params);
92 case LFUN_INSERT_BIBITEM:
93 return new InsetBibitem(InsetCommandParams("bibitem"));
95 case LFUN_INSET_FLOAT:
96 // check if the float type exists
97 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
98 return new InsetFloat(params, cmd.argument);
99 lyxerr << "Non-existent float type: " << cmd.argument << endl;
102 case LFUN_INSET_WIDE_FLOAT:
103 // check if the float type exists
104 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
105 InsetFloat * p = new InsetFloat(params, cmd.argument);
106 p->wide(true, params);
108 lyxerr << "Non-existent float type: " << cmd.argument << endl;
111 case LFUN_INSET_WRAP:
112 if (cmd.argument == "figure")
113 return new InsetWrap(params, cmd.argument);
114 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
117 case LFUN_INDEX_INSERT: {
118 // Try and generate a valid index entry.
119 InsetCommandParams icp("index");
120 string const contents = cmd.argument.empty() ?
121 bv->getLyXText()->getStringToIndex() :
123 icp.setContents(contents);
125 string data = InsetCommandMailer::params2string("index", icp);
126 LyXView * lv = bv->owner();
128 if (icp.getContents().empty()) {
129 lv->getDialogs().show("index", data, 0);
131 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
136 case LFUN_TABULAR_INSERT:
137 if (!cmd.argument.empty()) {
140 ::sscanf(cmd.argument.c_str(),"%d%d", &r, &c);
141 return new InsetTabular(*bv->buffer(), r, c);
143 bv->owner()->getDialogs().show("tabularcreate");
146 case LFUN_INSET_CAPTION:
147 if (bv->theLockingInset()) {
148 lyxerr << "Locking inset code: "
149 << static_cast<int>(bv->theLockingInset()->lyxCode());
150 InsetCaption * inset = new InsetCaption(params);
151 inset->setOwner(bv->theLockingInset());
152 inset->setAutoBreakRows(true);
153 inset->setDrawFrame(InsetText::LOCKED);
154 inset->setFrameColor(LColor::captionframe);
159 case LFUN_INDEX_PRINT:
160 return new InsetPrintIndex(InsetCommandParams("printindex"));
162 case LFUN_TOC_INSERT:
163 return new InsetTOC(InsetCommandParams("tableofcontents"));
165 case LFUN_ENVIRONMENT_INSERT:
166 return new InsetEnvironment(params, cmd.argument);
169 case LFUN_INSET_LIST:
170 return new InsetList;
172 case LFUN_INSET_THEOREM:
173 return new InsetTheorem;
176 case LFUN_INSET_INSERT: {
177 string const name = cmd.getArg(0);
179 if (name == "bibitem") {
180 InsetCommandParams icp;
181 InsetCommandMailer::string2params(cmd.argument, icp);
182 return new InsetBibitem(icp);
184 } else if (name == "bibtex") {
185 InsetCommandParams icp;
186 InsetCommandMailer::string2params(cmd.argument, icp);
187 return new InsetBibtex(icp);
189 } else if (name == "citation") {
190 InsetCommandParams icp;
191 InsetCommandMailer::string2params(cmd.argument, icp);
192 InsetCitation * inset = new InsetCitation(icp);
193 inset->setLoadingBuffer(bv->buffer(), false);
196 } else if (name == "ert") {
197 InsetERT * inset = new InsetERT(params);
198 InsetERT::ERTStatus s;
199 InsetERTMailer::string2params(cmd.argument, s);
200 inset->status(bv, s);
203 } else if (name == "external") {
204 Buffer const * buffer = cmd.view()->buffer();
205 InsetExternal::Params iep;
206 InsetExternalMailer::string2params(cmd.argument,
208 InsetExternal * inset = new InsetExternal;
209 inset->setParams(iep, buffer);
212 } else if (name == "graphics") {
213 Buffer const & buffer = *cmd.view()->buffer();
214 InsetGraphicsParams igp;
215 InsetGraphicsMailer::string2params(cmd.argument,
217 InsetGraphics * inset = new InsetGraphics;
218 inset->setParams(igp);
221 } else if (name == "include") {
222 InsetInclude::Params iip;
223 InsetIncludeMailer::string2params(cmd.argument, iip);
224 return new InsetInclude(iip);
226 } else if (name == "index") {
227 InsetCommandParams icp;
228 InsetCommandMailer::string2params(cmd.argument, icp);
229 return new InsetIndex(icp);
231 } else if (name == "label") {
232 InsetCommandParams icp;
233 InsetCommandMailer::string2params(cmd.argument, icp);
234 return new InsetLabel(icp);
236 } else if (name == "ref") {
237 InsetCommandParams icp;
238 InsetCommandMailer::string2params(cmd.argument, icp);
239 return new InsetRef(icp, *bv->buffer());
241 } else if (name == "toc") {
242 InsetCommandParams icp;
243 InsetCommandMailer::string2params(cmd.argument, icp);
244 return new InsetTOC(icp);
246 } else if (name == "url") {
247 InsetCommandParams icp;
248 InsetCommandMailer::string2params(cmd.argument, icp);
249 return new InsetUrl(icp);
253 case LFUN_SPACE_INSERT: {
254 string const name = cmd.argument;
255 if (name == "normal")
256 return new InsetSpace(InsetSpace::NORMAL);
257 else if (name == "protected")
258 return new InsetSpace(InsetSpace::PROTECTED);
259 else if (name == "thin")
260 return new InsetSpace(InsetSpace::THIN);
261 else if (name == "quad")
262 return new InsetSpace(InsetSpace::QUAD);
263 else if (name == "qquad")
264 return new InsetSpace(InsetSpace::QQUAD);
265 else if (name == "enspace")
266 return new InsetSpace(InsetSpace::ENSPACE);
267 else if (name == "enskip")
268 return new InsetSpace(InsetSpace::ENSKIP);
269 else if (name == "negthinspace")
270 return new InsetSpace(InsetSpace::NEGTHIN);
271 else if (name.empty())
272 lyxerr << "LyX function 'space' needs an argument." << endl;
274 lyxerr << "Wrong argument for LyX function 'space'." << endl;
287 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
290 if (lex.getString() != "\\begin_inset") {
291 lyxerr << "Buffer::readInset: Consistency check failed."
295 InsetOld * inset = 0;
298 string const tmptok = lex.getString();
300 // test the different insets
301 if (tmptok == "LatexCommand") {
302 InsetCommandParams inscmd;
305 string const cmdName = inscmd.getCmdName();
307 // This strange command allows LyX to recognize "natbib" style
308 // citations: citet, citep, Citet etc.
309 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
310 inset = new InsetCitation(inscmd);
311 } else if (cmdName == "bibitem") {
312 lex.printError("Wrong place for bibitem");
313 inset = new InsetBibitem(inscmd);
314 } else if (cmdName == "bibtex") {
315 inset = new InsetBibtex(inscmd);
316 } else if (cmdName == "index") {
317 inset = new InsetIndex(inscmd);
318 } else if (cmdName == "include") {
319 inset = new InsetInclude(inscmd, buf);
320 } else if (cmdName == "label") {
321 inset = new InsetLabel(inscmd);
322 } else if (cmdName == "url"
323 || cmdName == "htmlurl") {
324 inset = new InsetUrl(inscmd);
325 } else if (cmdName == "ref"
326 || cmdName == "eqref"
327 || cmdName == "pageref"
329 || cmdName == "vpageref"
330 || cmdName == "prettyref") {
331 if (!inscmd.getOptions().empty()
332 || !inscmd.getContents().empty()) {
333 inset = new InsetRef(inscmd, buf);
335 } else if (cmdName == "tableofcontents") {
336 inset = new InsetTOC(inscmd);
337 } else if (cmdName == "listofalgorithms") {
338 inset = new InsetFloatList("algorithm");
339 } else if (cmdName == "listoffigures") {
340 inset = new InsetFloatList("figure");
341 } else if (cmdName == "listoftables") {
342 inset = new InsetFloatList("table");
343 } else if (cmdName == "printindex") {
344 inset = new InsetPrintIndex(inscmd);
347 if (tmptok == "Quotes") {
348 inset = new InsetQuotes;
349 } else if (tmptok == "External") {
350 inset = new InsetExternal;
351 } else if (tmptok == "FormulaMacro") {
352 inset = new InsetFormulaMacro;
353 } else if (tmptok == "Formula") {
354 inset = new InsetFormula;
355 } else if (tmptok == "Graphics") {
356 inset = new InsetGraphics;
357 } else if (tmptok == "Note" || tmptok == "Comment"
358 || tmptok == "Greyedout") {
359 inset = new InsetNote(buf.params, tmptok);
360 } else if (tmptok == "Include") {
361 InsetCommandParams p("Include");
362 inset = new InsetInclude(p, buf);
363 } else if (tmptok == "Environment") {
365 inset = new InsetEnvironment(buf.params, lex.getString());
366 } else if (tmptok == "ERT") {
367 inset = new InsetERT(buf.params);
368 } else if (tmptok == "InsetSpace") {
369 inset = new InsetSpace;
370 } else if (tmptok == "Tabular") {
371 inset = new InsetTabular(buf);
372 } else if (tmptok == "Text") {
373 inset = new InsetText(buf.params);
374 } else if (tmptok == "Foot") {
375 inset = new InsetFoot(buf.params);
376 } else if (tmptok == "Marginal") {
377 inset = new InsetMarginal(buf.params);
378 } else if (tmptok == "OptArg") {
379 inset = new InsetOptArg(buf.params);
380 } else if (tmptok == "Minipage") {
381 inset = new InsetMinipage(buf.params);
382 } else if (tmptok == "Float") {
384 string tmptok = lex.getString();
385 inset = new InsetFloat(buf.params, tmptok);
386 } else if (tmptok == "Wrap") {
388 string tmptok = lex.getString();
389 inset = new InsetWrap(buf.params, tmptok);
391 } else if (tmptok == "List") {
392 inset = new InsetList;
393 } else if (tmptok == "Theorem") {
394 inset = new InsetList;
396 } else if (tmptok == "Caption") {
397 inset = new InsetCaption(buf.params);
398 } else if (tmptok == "FloatList") {
399 inset = new InsetFloatList;
403 inset->read(&buf, lex);