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/insetcharstyle.h"
30 #include "insets/insetenv.h"
31 #include "insets/insetert.h"
32 #include "insets/insetexternal.h"
33 #include "insets/insetfloat.h"
34 #include "insets/insetfloatlist.h"
35 #include "insets/insetfoot.h"
36 #include "insets/insetgraphics.h"
37 #include "insets/insethfill.h"
38 #include "insets/insetinclude.h"
39 #include "insets/insetindex.h"
40 #include "insets/insetlabel.h"
41 #include "insets/insetline.h"
42 #include "insets/insetmarginal.h"
43 #include "insets/insetminipage.h"
44 #include "insets/insetnote.h"
45 #include "insets/insetbox.h"
46 #include "insets/insetbranch.h"
47 #include "insets/insetoptarg.h"
48 #include "insets/insetpagebreak.h"
49 #include "insets/insetref.h"
50 #include "insets/insetspace.h"
51 #include "insets/insettabular.h"
52 #include "insets/insettoc.h"
53 #include "insets/inseturl.h"
54 #include "insets/insetvspace.h"
55 #include "insets/insetwrap.h"
56 #include "mathed/formulamacro.h"
57 #include "mathed/formula.h"
59 #include "frontends/Dialogs.h"
60 #include "frontends/LyXView.h"
61 #include "support/lstrings.h"
62 #include "support/std_sstream.h"
64 #include <boost/assert.hpp>
66 using lyx::support::compare_ascii_no_case;
73 InsetOld * createInset(FuncRequest const & cmd)
75 BufferView * bv = cmd.view();
76 BufferParams const & params = bv->buffer()->params();
80 return new InsetHFill;
82 case LFUN_INSERT_LINE:
85 case LFUN_INSERT_PAGEBREAK:
86 return new InsetPagebreak;
88 case LFUN_INSET_MINIPAGE:
89 return new InsetMinipage(params);
91 case LFUN_INSERT_CHARSTYLE: {
92 string s = cmd.getArg(0);
93 CharStyles::iterator found_cs = params.getLyXTextClass().charstyle(s);
94 return new InsetCharStyle(params, found_cs);
97 case LFUN_INSERT_NOTE: {
98 string arg = cmd.getArg(0);
101 return new InsetNote(params, arg);
104 case LFUN_INSERT_BOX: {
105 string arg = cmd.getArg(0);
108 return new InsetBox(params, arg);
111 case LFUN_INSERT_BRANCH: {
112 string arg = cmd.getArg(0);
115 return new InsetBranch(params, arg);
119 return new InsetERT(params);
121 case LFUN_INSET_FOOTNOTE:
122 return new InsetFoot(params);
124 case LFUN_INSET_MARGINAL:
125 return new InsetMarginal(params);
127 case LFUN_INSET_OPTARG:
128 return new InsetOptArg(params);
130 case LFUN_INSERT_BIBITEM:
131 return new InsetBibitem(InsetCommandParams("bibitem"));
133 case LFUN_INSET_FLOAT:
134 // check if the float type exists
135 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
136 return new InsetFloat(params, cmd.argument);
137 lyxerr << "Non-existent float type: " << cmd.argument << endl;
140 case LFUN_INSET_WIDE_FLOAT:
141 // check if the float type exists
142 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
143 auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
144 p->wide(true, params);
147 lyxerr << "Non-existent float type: " << cmd.argument << endl;
150 case LFUN_INSET_WRAP:
151 if (cmd.argument == "figure")
152 return new InsetWrap(params, cmd.argument);
153 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
156 case LFUN_INDEX_INSERT: {
157 // Try and generate a valid index entry.
158 InsetCommandParams icp("index");
159 string const contents = cmd.argument.empty() ?
160 bv->getLyXText()->getStringToIndex() :
162 icp.setContents(contents);
164 string data = InsetCommandMailer::params2string("index", icp);
165 LyXView * lv = bv->owner();
167 if (icp.getContents().empty()) {
168 lv->getDialogs().show("index", data, 0);
170 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
175 case LFUN_TABULAR_INSERT:
176 if (!cmd.argument.empty()) {
177 std::istringstream ss(cmd.argument);
182 return new InsetTabular(*bv->buffer(), r, c);
184 bv->owner()->getDialogs().show("tabularcreate");
187 case LFUN_INSET_CAPTION:
188 if (!bv->innerInset()) {
189 auto_ptr<InsetCaption> inset(new InsetCaption(params));
190 inset->setOwner(bv->innerInset());
191 inset->setAutoBreakRows(true);
192 inset->setDrawFrame(InsetText::LOCKED);
193 inset->setFrameColor(LColor::captionframe);
194 return inset.release();
198 case LFUN_INDEX_PRINT:
199 return new InsetPrintIndex(InsetCommandParams("printindex"));
201 case LFUN_TOC_INSERT:
202 return new InsetTOC(InsetCommandParams("tableofcontents"));
204 case LFUN_ENVIRONMENT_INSERT:
205 return new InsetEnvironment(params, cmd.argument);
208 case LFUN_INSET_LIST:
209 return new InsetList;
211 case LFUN_INSET_THEOREM:
212 return new InsetTheorem;
215 case LFUN_INSET_INSERT: {
216 string const name = cmd.getArg(0);
218 if (name == "bibitem") {
219 InsetCommandParams icp;
220 InsetCommandMailer::string2params(cmd.argument, icp);
221 return new InsetBibitem(icp);
223 } else if (name == "bibtex") {
224 InsetCommandParams icp;
225 InsetCommandMailer::string2params(cmd.argument, icp);
226 return new InsetBibtex(icp);
228 } else if (name == "citation") {
229 InsetCommandParams icp;
230 InsetCommandMailer::string2params(cmd.argument, icp);
231 return new InsetCitation(icp);
233 } else if (name == "ert") {
234 InsetCollapsable::CollapseStatus st;
235 InsetERTMailer::string2params(cmd.argument, st);
236 return new InsetERT(params, st);
238 } else if (name == "external") {
239 Buffer const & buffer = *cmd.view()->buffer();
240 InsetExternalParams iep;
241 InsetExternalMailer::string2params(cmd.argument,
243 auto_ptr<InsetExternal> inset(new InsetExternal);
244 inset->setParams(iep, buffer);
245 return inset.release();
247 } else if (name == "graphics") {
248 Buffer const & buffer = *cmd.view()->buffer();
249 InsetGraphicsParams igp;
250 InsetGraphicsMailer::string2params(cmd.argument,
252 auto_ptr<InsetGraphics> inset(new InsetGraphics);
253 inset->setParams(igp);
254 return inset.release();
256 } else if (name == "include") {
257 InsetCommandParams iip;
258 InsetIncludeMailer::string2params(cmd.argument, iip);
259 return new InsetInclude(iip);
261 } else if (name == "index") {
262 InsetCommandParams icp;
263 InsetCommandMailer::string2params(cmd.argument, icp);
264 return new InsetIndex(icp);
266 } else if (name == "label") {
267 InsetCommandParams icp;
268 InsetCommandMailer::string2params(cmd.argument, icp);
269 return new InsetLabel(icp);
271 } else if (name == "ref") {
272 InsetCommandParams icp;
273 InsetCommandMailer::string2params(cmd.argument, icp);
274 return new InsetRef(icp, *bv->buffer());
276 } else if (name == "toc") {
277 InsetCommandParams icp;
278 InsetCommandMailer::string2params(cmd.argument, icp);
279 return new InsetTOC(icp);
281 } else if (name == "url") {
282 InsetCommandParams icp;
283 InsetCommandMailer::string2params(cmd.argument, icp);
284 return new InsetUrl(icp);
286 } else if (name == "vspace") {
288 InsetVSpaceMailer::string2params(cmd.argument, vspace);
289 return new InsetVSpace(vspace);
293 case LFUN_SPACE_INSERT: {
294 string const name = cmd.argument;
295 if (name == "normal")
296 return new InsetSpace(InsetSpace::NORMAL);
297 else if (name == "protected")
298 return new InsetSpace(InsetSpace::PROTECTED);
299 else if (name == "thin")
300 return new InsetSpace(InsetSpace::THIN);
301 else if (name == "quad")
302 return new InsetSpace(InsetSpace::QUAD);
303 else if (name == "qquad")
304 return new InsetSpace(InsetSpace::QQUAD);
305 else if (name == "enspace")
306 return new InsetSpace(InsetSpace::ENSPACE);
307 else if (name == "enskip")
308 return new InsetSpace(InsetSpace::ENSKIP);
309 else if (name == "negthinspace")
310 return new InsetSpace(InsetSpace::NEGTHIN);
311 else if (name.empty())
312 lyxerr << "LyX function 'space' needs an argument." << endl;
314 lyxerr << "Wrong argument for LyX function 'space'." << endl;
327 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
330 if (lex.getString() != "\\begin_inset") {
331 lyxerr << "Buffer::readInset: Consistency check failed."
335 auto_ptr<InsetOld> inset;
337 LyXTextClass tclass = buf.params().getLyXTextClass();
340 string tmptok = lex.getString();
342 // test the different insets
343 if (tmptok == "LatexCommand") {
344 InsetCommandParams inscmd;
347 string const cmdName = inscmd.getCmdName();
349 // This strange command allows LyX to recognize "natbib" style
350 // citations: citet, citep, Citet etc.
351 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
352 inset.reset(new InsetCitation(inscmd));
353 } else if (cmdName == "bibitem") {
354 lex.printError("Wrong place for bibitem");
355 inset.reset(new InsetBibitem(inscmd));
356 } else if (cmdName == "bibtex") {
357 inset.reset(new InsetBibtex(inscmd));
358 } else if (cmdName == "index") {
359 inset.reset(new InsetIndex(inscmd));
360 } else if (cmdName == "include") {
361 inset.reset(new InsetInclude(inscmd));
362 } else if (cmdName == "label") {
363 inset.reset(new InsetLabel(inscmd));
364 } else if (cmdName == "url"
365 || cmdName == "htmlurl") {
366 inset.reset(new InsetUrl(inscmd));
367 } else if (cmdName == "ref"
368 || cmdName == "eqref"
369 || cmdName == "pageref"
371 || cmdName == "vpageref"
372 || cmdName == "prettyref") {
373 if (!inscmd.getOptions().empty()
374 || !inscmd.getContents().empty()) {
375 inset.reset(new InsetRef(inscmd, buf));
377 } else if (cmdName == "tableofcontents") {
378 inset.reset(new InsetTOC(inscmd));
379 } else if (cmdName == "listofalgorithms") {
380 inset.reset(new InsetFloatList("algorithm"));
381 } else if (cmdName == "listoffigures") {
382 inset.reset(new InsetFloatList("figure"));
383 } else if (cmdName == "listoftables") {
384 inset.reset(new InsetFloatList("table"));
385 } else if (cmdName == "printindex") {
386 inset.reset(new InsetPrintIndex(inscmd));
388 lyxerr << "unknown CommandInset '" << cmdName
390 while (lex.isOK() && lex.getString() != "\\end_inset")
395 if (tmptok == "Quotes") {
396 inset.reset(new InsetQuotes);
397 } else if (tmptok == "External") {
398 inset.reset(new InsetExternal);
399 } else if (tmptok == "FormulaMacro") {
400 inset.reset(new InsetFormulaMacro);
401 } else if (tmptok == "Formula") {
402 inset.reset(new InsetFormula);
403 } else if (tmptok == "Graphics") {
404 inset.reset(new InsetGraphics);
405 } else if (tmptok == "Note" || tmptok == "Comment"
406 || tmptok == "Greyedout") {
407 inset.reset(new InsetNote(buf.params(), tmptok));
408 } else if (tmptok == "Boxed" || tmptok == "ovalbox"
409 || tmptok == "Shadowbox" || tmptok == "Doublebox"
410 || tmptok == "Ovalbox" || tmptok == "Frameless") {
411 inset.reset(new InsetBox(buf.params(), tmptok));
412 } else if (tmptok == "CharStyle") {
414 string s = lex.getString();
415 CharStyles::iterator found_cs = tclass.charstyle(s);
416 inset.reset(new InsetCharStyle(buf.params(), found_cs));
417 } else if (tmptok == "Branch") {
418 inset.reset(new InsetBranch(buf.params(), string()));
419 } else if (tmptok == "Include") {
420 InsetCommandParams p("Include");
421 inset.reset(new InsetInclude(p));
422 } else if (tmptok == "Environment") {
424 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
425 } else if (tmptok == "ERT") {
426 inset.reset(new InsetERT(buf.params()));
427 } else if (tmptok == "InsetSpace") {
428 inset.reset(new InsetSpace);
429 } else if (tmptok == "Tabular") {
430 inset.reset(new InsetTabular(buf));
431 } else if (tmptok == "Text") {
432 inset.reset(new InsetText(buf.params()));
433 } else if (tmptok == "VSpace") {
434 inset.reset(new InsetVSpace);
435 } else if (tmptok == "Foot") {
436 inset.reset(new InsetFoot(buf.params()));
437 } else if (tmptok == "Marginal") {
438 inset.reset(new InsetMarginal(buf.params()));
439 } else if (tmptok == "OptArg") {
440 inset.reset(new InsetOptArg(buf.params()));
441 } else if (tmptok == "Minipage") {
442 inset.reset(new InsetMinipage(buf.params()));
443 } else if (tmptok == "Float") {
445 string tmptok = lex.getString();
446 inset.reset(new InsetFloat(buf.params(), tmptok));
447 } else if (tmptok == "Wrap") {
449 string tmptok = lex.getString();
450 inset.reset(new InsetWrap(buf.params(), tmptok));
452 } else if (tmptok == "List") {
453 inset.reset(new InsetList);
454 } else if (tmptok == "Theorem") {
455 inset.reset(new InsetList);
457 } else if (tmptok == "Caption") {
458 inset.reset(new InsetCaption(buf.params()));
459 } else if (tmptok == "FloatList") {
460 inset.reset(new InsetFloatList);
462 lyxerr << "unknown Inset type '" << tmptok
464 while (lex.isOK() && lex.getString() != "\\end_inset")
469 inset->read(buf, lex);
472 return inset.release();