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"
23 #include "paragraph.h"
25 #include "insets/insetbibitem.h"
26 #include "insets/insetbibtex.h"
27 #include "insets/insetcaption.h"
28 #include "insets/insetcite.h"
29 #include "insets/insetenv.h"
30 #include "insets/insetert.h"
31 #include "insets/insetexternal.h"
32 #include "insets/insetfloat.h"
33 #include "insets/insetfloatlist.h"
34 #include "insets/insetfoot.h"
35 #include "insets/insetgraphics.h"
36 #include "insets/insethfill.h"
37 #include "insets/insetinclude.h"
38 #include "insets/insetindex.h"
39 #include "insets/insetlabel.h"
40 #include "insets/insetline.h"
41 #include "insets/insetmarginal.h"
42 #include "insets/insetminipage.h"
43 #include "insets/insetnote.h"
44 #include "insets/insetbox.h"
45 #include "insets/insetbranch.h"
46 #include "insets/insetoptarg.h"
47 #include "insets/insetpagebreak.h"
48 #include "insets/insetref.h"
49 #include "insets/insetspace.h"
50 #include "insets/insettabular.h"
51 #include "insets/insettoc.h"
52 #include "insets/inseturl.h"
53 #include "insets/insetwrap.h"
54 #include "mathed/formulamacro.h"
55 #include "mathed/formula.h"
57 #include "frontends/Dialogs.h"
58 #include "frontends/LyXView.h"
59 #include "support/lstrings.h"
60 #include "support/std_sstream.h"
62 #include <boost/assert.hpp>
64 using lyx::support::compare_ascii_no_case;
71 InsetOld * createInset(FuncRequest const & cmd)
73 BufferView * bv = cmd.view();
74 BufferParams const & params = bv->buffer()->params();
78 return new InsetHFill;
80 case LFUN_INSERT_LINE:
83 case LFUN_INSERT_PAGEBREAK:
84 return new InsetPagebreak;
86 case LFUN_INSET_MINIPAGE:
87 return new InsetMinipage(params);
89 case LFUN_INSERT_NOTE: {
90 string arg = cmd.getArg(0);
93 return new InsetNote(params, arg);
96 case LFUN_INSERT_BOX: {
97 string arg = cmd.getArg(0);
100 return new InsetBox(params, arg);
103 case LFUN_INSERT_BRANCH: {
104 string arg = cmd.getArg(0);
107 return new InsetBranch(params, arg);
111 return new InsetERT(params);
113 case LFUN_INSET_FOOTNOTE:
114 return new InsetFoot(params);
116 case LFUN_INSET_MARGINAL:
117 return new InsetMarginal(params);
119 case LFUN_INSET_OPTARG:
120 return new InsetOptArg(params);
122 case LFUN_INSERT_BIBITEM:
123 return new InsetBibitem(InsetCommandParams("bibitem"));
125 case LFUN_INSET_FLOAT:
126 // check if the float type exists
127 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
128 return new InsetFloat(params, cmd.argument);
129 lyxerr << "Non-existent float type: " << cmd.argument << endl;
132 case LFUN_INSET_WIDE_FLOAT:
133 // check if the float type exists
134 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
135 auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
136 p->wide(true, params);
139 lyxerr << "Non-existent float type: " << cmd.argument << endl;
142 case LFUN_INSET_WRAP:
143 if (cmd.argument == "figure")
144 return new InsetWrap(params, cmd.argument);
145 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
148 case LFUN_INDEX_INSERT: {
149 // Try and generate a valid index entry.
150 InsetCommandParams icp("index");
151 string const contents = cmd.argument.empty() ?
152 bv->getLyXText()->getStringToIndex() :
154 icp.setContents(contents);
156 string data = InsetCommandMailer::params2string("index", icp);
157 LyXView * lv = bv->owner();
159 if (icp.getContents().empty()) {
160 lv->getDialogs().show("index", data, 0);
162 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
167 case LFUN_TABULAR_INSERT:
168 if (!cmd.argument.empty()) {
169 std::istringstream ss(cmd.argument);
174 return new InsetTabular(*bv->buffer(), r, c);
176 bv->owner()->getDialogs().show("tabularcreate");
179 case LFUN_INSET_CAPTION:
180 if (bv->innerInset()) {
181 auto_ptr<InsetCaption> inset(new InsetCaption(params));
182 inset->setOwner(bv->innerInset());
183 inset->setAutoBreakRows(true);
184 inset->setDrawFrame(InsetText::LOCKED);
185 inset->setFrameColor(LColor::captionframe);
186 return inset.release();
189 case LFUN_INDEX_PRINT:
190 return new InsetPrintIndex(InsetCommandParams("printindex"));
192 case LFUN_TOC_INSERT:
193 return new InsetTOC(InsetCommandParams("tableofcontents"));
195 case LFUN_ENVIRONMENT_INSERT:
196 return new InsetEnvironment(params, cmd.argument);
199 case LFUN_INSET_LIST:
200 return new InsetList;
202 case LFUN_INSET_THEOREM:
203 return new InsetTheorem;
206 case LFUN_INSET_INSERT: {
207 string const name = cmd.getArg(0);
209 if (name == "bibitem") {
210 InsetCommandParams icp;
211 InsetCommandMailer::string2params(cmd.argument, icp);
212 return new InsetBibitem(icp);
214 } else if (name == "bibtex") {
215 InsetCommandParams icp;
216 InsetCommandMailer::string2params(cmd.argument, icp);
217 return new InsetBibtex(icp);
219 } else if (name == "citation") {
220 InsetCommandParams icp;
221 InsetCommandMailer::string2params(cmd.argument, icp);
222 return new InsetCitation(icp);
224 } else if (name == "ert") {
225 InsetERT * inset = new InsetERT(params);
226 InsetERT::ERTStatus s;
227 InsetERTMailer::string2params(cmd.argument, s);
228 inset->status(bv, s);
231 } else if (name == "external") {
232 Buffer const & buffer = *cmd.view()->buffer();
233 InsetExternalParams iep;
234 InsetExternalMailer::string2params(cmd.argument,
236 auto_ptr<InsetExternal> inset(new InsetExternal);
237 inset->setParams(iep, buffer);
238 return inset.release();
240 } else if (name == "graphics") {
241 Buffer const & buffer = *cmd.view()->buffer();
242 InsetGraphicsParams igp;
243 InsetGraphicsMailer::string2params(cmd.argument,
245 auto_ptr<InsetGraphics> inset(new InsetGraphics);
246 inset->setParams(igp);
247 return inset.release();
249 } else if (name == "include") {
250 InsetCommandParams iip;
251 InsetIncludeMailer::string2params(cmd.argument, iip);
252 return new InsetInclude(iip);
254 } else if (name == "index") {
255 InsetCommandParams icp;
256 InsetCommandMailer::string2params(cmd.argument, icp);
257 return new InsetIndex(icp);
259 } else if (name == "label") {
260 InsetCommandParams icp;
261 InsetCommandMailer::string2params(cmd.argument, icp);
262 return new InsetLabel(icp);
264 } else if (name == "ref") {
265 InsetCommandParams icp;
266 InsetCommandMailer::string2params(cmd.argument, icp);
267 return new InsetRef(icp, *bv->buffer());
269 } else if (name == "toc") {
270 InsetCommandParams icp;
271 InsetCommandMailer::string2params(cmd.argument, icp);
272 return new InsetTOC(icp);
274 } else if (name == "url") {
275 InsetCommandParams icp;
276 InsetCommandMailer::string2params(cmd.argument, icp);
277 return new InsetUrl(icp);
281 case LFUN_INSERT_LABEL: {
282 InsetCommandParams icp;
283 InsetCommandMailer::string2params(cmd.argument, icp);
284 return new InsetLabel(icp);
289 InsetCommandParams icp;
290 InsetCommandMailer::string2params(cmd.argument, icp);
291 return new InsetUrl(icp);
294 case LFUN_SPACE_INSERT: {
295 string const name = cmd.argument;
296 if (name == "normal")
297 return new InsetSpace(InsetSpace::NORMAL);
298 else if (name == "protected")
299 return new InsetSpace(InsetSpace::PROTECTED);
300 else if (name == "thin")
301 return new InsetSpace(InsetSpace::THIN);
302 else if (name == "quad")
303 return new InsetSpace(InsetSpace::QUAD);
304 else if (name == "qquad")
305 return new InsetSpace(InsetSpace::QQUAD);
306 else if (name == "enspace")
307 return new InsetSpace(InsetSpace::ENSPACE);
308 else if (name == "enskip")
309 return new InsetSpace(InsetSpace::ENSKIP);
310 else if (name == "negthinspace")
311 return new InsetSpace(InsetSpace::NEGTHIN);
312 else if (name.empty())
313 lyxerr << "LyX function 'space' needs an argument." << endl;
315 lyxerr << "Wrong argument for LyX function 'space'." << endl;
328 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
331 if (lex.getString() != "\\begin_inset") {
332 lyxerr << "Buffer::readInset: Consistency check failed."
336 auto_ptr<InsetOld> inset;
339 string const tmptok = lex.getString();
341 // test the different insets
342 if (tmptok == "LatexCommand") {
343 InsetCommandParams inscmd;
346 string const cmdName = inscmd.getCmdName();
348 // This strange command allows LyX to recognize "natbib" style
349 // citations: citet, citep, Citet etc.
350 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
351 inset.reset(new InsetCitation(inscmd));
352 } else if (cmdName == "bibitem") {
353 lex.printError("Wrong place for bibitem");
354 inset.reset(new InsetBibitem(inscmd));
355 } else if (cmdName == "bibtex") {
356 inset.reset(new InsetBibtex(inscmd));
357 } else if (cmdName == "index") {
358 inset.reset(new InsetIndex(inscmd));
359 } else if (cmdName == "include") {
360 inset.reset(new InsetInclude(inscmd));
361 } else if (cmdName == "label") {
362 inset.reset(new InsetLabel(inscmd));
363 } else if (cmdName == "url"
364 || cmdName == "htmlurl") {
365 inset.reset(new InsetUrl(inscmd));
366 } else if (cmdName == "ref"
367 || cmdName == "eqref"
368 || cmdName == "pageref"
370 || cmdName == "vpageref"
371 || cmdName == "prettyref") {
372 if (!inscmd.getOptions().empty()
373 || !inscmd.getContents().empty()) {
374 inset.reset(new InsetRef(inscmd, buf));
376 } else if (cmdName == "tableofcontents") {
377 inset.reset(new InsetTOC(inscmd));
378 } else if (cmdName == "listofalgorithms") {
379 inset.reset(new InsetFloatList("algorithm"));
380 } else if (cmdName == "listoffigures") {
381 inset.reset(new InsetFloatList("figure"));
382 } else if (cmdName == "listoftables") {
383 inset.reset(new InsetFloatList("table"));
384 } else if (cmdName == "printindex") {
385 inset.reset(new InsetPrintIndex(inscmd));
387 lyxerr << "unknown CommandInset '" << cmdName
389 while (lex.isOK() && lex.getString() != "\\end_inset")
394 if (tmptok == "Quotes") {
395 inset.reset(new InsetQuotes);
396 } else if (tmptok == "External") {
397 inset.reset(new InsetExternal);
398 } else if (tmptok == "FormulaMacro") {
399 inset.reset(new InsetFormulaMacro);
400 } else if (tmptok == "Formula") {
401 inset.reset(new InsetFormula);
402 } else if (tmptok == "Graphics") {
403 inset.reset(new InsetGraphics);
404 } else if (tmptok == "Note" || tmptok == "Comment"
405 || tmptok == "Greyedout") {
406 inset.reset(new InsetNote(buf.params(), tmptok));
407 } else if (tmptok == "Boxed" || tmptok == "ovalbox"
408 || tmptok == "Shadowbox" || tmptok == "Doublebox"
409 || tmptok == "Ovalbox" || tmptok == "Frameless") {
410 inset.reset(new InsetBox(buf.params(), tmptok));
411 } else if (tmptok == "Branch") {
412 inset.reset(new InsetBranch(buf.params(), string()));
413 } else if (tmptok == "Include") {
414 InsetCommandParams p("Include");
415 inset.reset(new InsetInclude(p));
416 } else if (tmptok == "Environment") {
418 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
419 } else if (tmptok == "ERT") {
420 inset.reset(new InsetERT(buf.params()));
421 } else if (tmptok == "InsetSpace") {
422 inset.reset(new InsetSpace);
423 } else if (tmptok == "Tabular") {
424 inset.reset(new InsetTabular(buf));
425 } else if (tmptok == "Text") {
426 inset.reset(new InsetText(buf.params()));
427 } else if (tmptok == "Foot") {
428 inset.reset(new InsetFoot(buf.params()));
429 } else if (tmptok == "Marginal") {
430 inset.reset(new InsetMarginal(buf.params()));
431 } else if (tmptok == "OptArg") {
432 inset.reset(new InsetOptArg(buf.params()));
433 } else if (tmptok == "Minipage") {
434 inset.reset(new InsetMinipage(buf.params()));
435 } else if (tmptok == "Float") {
437 string tmptok = lex.getString();
438 inset.reset(new InsetFloat(buf.params(), tmptok));
439 } else if (tmptok == "Wrap") {
441 string tmptok = lex.getString();
442 inset.reset(new InsetWrap(buf.params(), tmptok));
444 } else if (tmptok == "List") {
445 inset.reset(new InsetList);
446 } else if (tmptok == "Theorem") {
447 inset.reset(new InsetList);
449 } else if (tmptok == "Caption") {
450 inset.reset(new InsetCaption(buf.params()));
451 } else if (tmptok == "FloatList") {
452 inset.reset(new InsetFloatList);
454 lyxerr << "unknown Inset type '" << tmptok
456 while (lex.isOK() && lex.getString() != "\\end_inset")
461 inset->read(buf, lex);
464 return inset.release();