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/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/insetvspace.h"
54 #include "insets/insetwrap.h"
55 #include "mathed/formulamacro.h"
56 #include "mathed/formula.h"
58 #include "frontends/Dialogs.h"
59 #include "frontends/LyXView.h"
60 #include "support/lstrings.h"
61 #include "support/std_sstream.h"
63 #include <boost/assert.hpp>
65 using lyx::support::compare_ascii_no_case;
72 InsetOld * createInset(FuncRequest const & cmd)
74 BufferView * bv = cmd.view();
75 BufferParams const & params = bv->buffer()->params();
79 return new InsetHFill;
81 case LFUN_INSERT_LINE:
84 case LFUN_INSERT_PAGEBREAK:
85 return new InsetPagebreak;
87 case LFUN_INSERT_CHARSTYLE: {
88 string s = cmd.getArg(0);
89 CharStyles::iterator found_cs = params.getLyXTextClass().charstyle(s);
90 return new InsetCharStyle(params, found_cs);
93 case LFUN_INSERT_NOTE: {
94 string arg = cmd.getArg(0);
97 return new InsetNote(params, arg);
100 case LFUN_INSERT_BOX: {
101 string arg = cmd.getArg(0);
104 return new InsetBox(params, arg);
107 case LFUN_INSERT_BRANCH: {
108 string arg = cmd.getArg(0);
111 return new InsetBranch(params, InsetBranchParams(arg));
115 return new InsetERT(params);
117 case LFUN_INSET_FOOTNOTE:
118 return new InsetFoot(params);
120 case LFUN_INSET_MARGINAL:
121 return new InsetMarginal(params);
123 case LFUN_INSET_OPTARG:
124 return new InsetOptArg(params);
126 case LFUN_INSERT_BIBITEM:
127 return new InsetBibitem(InsetCommandParams("bibitem"));
129 case LFUN_INSET_FLOAT:
130 // check if the float type exists
131 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
132 return new InsetFloat(params, cmd.argument);
133 lyxerr << "Non-existent float type: " << cmd.argument << endl;
136 case LFUN_INSET_WIDE_FLOAT:
137 // check if the float type exists
138 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
139 auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
140 p->wide(true, params);
143 lyxerr << "Non-existent float type: " << cmd.argument << endl;
146 case LFUN_INSET_WRAP:
147 if (cmd.argument == "figure")
148 return new InsetWrap(params, cmd.argument);
149 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
152 case LFUN_INDEX_INSERT: {
153 // Try and generate a valid index entry.
154 InsetCommandParams icp("index");
155 string const contents = cmd.argument.empty() ?
156 bv->getLyXText()->getStringToIndex() :
158 icp.setContents(contents);
160 string data = InsetCommandMailer::params2string("index", icp);
161 LyXView * lv = bv->owner();
163 if (icp.getContents().empty()) {
164 lv->getDialogs().show("index", data, 0);
166 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
171 case LFUN_TABULAR_INSERT:
172 if (!cmd.argument.empty()) {
173 std::istringstream ss(cmd.argument);
178 return new InsetTabular(*bv->buffer(), r, c);
180 bv->owner()->getDialogs().show("tabularcreate");
183 case LFUN_INSET_CAPTION:
184 if (!bv->innerInset()) {
185 auto_ptr<InsetCaption> inset(new InsetCaption(params));
186 inset->setOwner(bv->innerInset());
187 inset->setAutoBreakRows(true);
188 inset->setDrawFrame(InsetText::LOCKED);
189 inset->setFrameColor(LColor::captionframe);
190 return inset.release();
194 case LFUN_INDEX_PRINT:
195 return new InsetPrintIndex(InsetCommandParams("printindex"));
197 case LFUN_TOC_INSERT:
198 return new InsetTOC(InsetCommandParams("tableofcontents"));
200 case LFUN_ENVIRONMENT_INSERT:
201 return new InsetEnvironment(params, cmd.argument);
204 case LFUN_INSET_LIST:
205 return new InsetList;
207 case LFUN_INSET_THEOREM:
208 return new InsetTheorem;
211 case LFUN_INSET_INSERT: {
212 string const name = cmd.getArg(0);
214 if (name == "bibitem") {
215 InsetCommandParams icp;
216 InsetCommandMailer::string2params(name, cmd.argument,
218 return new InsetBibitem(icp);
220 } else if (name == "bibtex") {
221 InsetCommandParams icp;
222 InsetCommandMailer::string2params(name, cmd.argument,
224 return new InsetBibtex(icp);
226 } else if (name == "citation") {
227 InsetCommandParams icp;
228 InsetCommandMailer::string2params(name, cmd.argument,
230 return new InsetCitation(icp);
232 } else if (name == "ert") {
233 InsetCollapsable::CollapseStatus st;
234 InsetERTMailer::string2params(cmd.argument, st);
235 return new InsetERT(params, st);
237 } else if (name == "external") {
238 Buffer const & buffer = *cmd.view()->buffer();
239 InsetExternalParams iep;
240 InsetExternalMailer::string2params(cmd.argument,
242 auto_ptr<InsetExternal> inset(new InsetExternal);
243 inset->setParams(iep, buffer);
244 return inset.release();
246 } else if (name == "graphics") {
247 Buffer const & buffer = *cmd.view()->buffer();
248 InsetGraphicsParams igp;
249 InsetGraphicsMailer::string2params(cmd.argument,
251 auto_ptr<InsetGraphics> inset(new InsetGraphics);
252 inset->setParams(igp);
253 return inset.release();
255 } else if (name == "include") {
256 InsetCommandParams iip;
257 InsetIncludeMailer::string2params(cmd.argument, iip);
258 return new InsetInclude(iip);
260 } else if (name == "index") {
261 InsetCommandParams icp;
262 InsetCommandMailer::string2params(name, cmd.argument,
264 return new InsetIndex(icp);
266 } else if (name == "label") {
267 InsetCommandParams icp;
268 InsetCommandMailer::string2params(name, cmd.argument,
270 return new InsetLabel(icp);
272 } else if (name == "ref") {
273 InsetCommandParams icp;
274 InsetCommandMailer::string2params(name, cmd.argument,
276 return new InsetRef(icp, *bv->buffer());
278 } else if (name == "toc") {
279 InsetCommandParams icp;
280 InsetCommandMailer::string2params(name, cmd.argument,
282 return new InsetTOC(icp);
284 } else if (name == "url") {
285 InsetCommandParams icp;
286 InsetCommandMailer::string2params(name, cmd.argument,
288 return new InsetUrl(icp);
290 } else if (name == "vspace") {
292 InsetVSpaceMailer::string2params(cmd.argument, vspace);
293 return new InsetVSpace(vspace);
297 case LFUN_SPACE_INSERT: {
298 string const name = cmd.argument;
299 if (name == "normal")
300 return new InsetSpace(InsetSpace::NORMAL);
301 else if (name == "protected")
302 return new InsetSpace(InsetSpace::PROTECTED);
303 else if (name == "thin")
304 return new InsetSpace(InsetSpace::THIN);
305 else if (name == "quad")
306 return new InsetSpace(InsetSpace::QUAD);
307 else if (name == "qquad")
308 return new InsetSpace(InsetSpace::QQUAD);
309 else if (name == "enspace")
310 return new InsetSpace(InsetSpace::ENSPACE);
311 else if (name == "enskip")
312 return new InsetSpace(InsetSpace::ENSKIP);
313 else if (name == "negthinspace")
314 return new InsetSpace(InsetSpace::NEGTHIN);
315 else if (name.empty())
316 lyxerr << "LyX function 'space' needs an argument." << endl;
318 lyxerr << "Wrong argument for LyX function 'space'." << endl;
331 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
334 if (lex.getString() != "\\begin_inset") {
335 lyxerr << "Buffer::readInset: Consistency check failed."
339 auto_ptr<InsetOld> inset;
341 LyXTextClass tclass = buf.params().getLyXTextClass();
344 string tmptok = lex.getString();
346 // test the different insets
347 if (tmptok == "LatexCommand") {
348 InsetCommandParams inscmd;
351 string const cmdName = inscmd.getCmdName();
353 // This strange command allows LyX to recognize "natbib" style
354 // citations: citet, citep, Citet etc.
355 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
356 inset.reset(new InsetCitation(inscmd));
357 } else if (cmdName == "bibitem") {
358 lex.printError("Wrong place for bibitem");
359 inset.reset(new InsetBibitem(inscmd));
360 } else if (cmdName == "bibtex") {
361 inset.reset(new InsetBibtex(inscmd));
362 } else if (cmdName == "index") {
363 inset.reset(new InsetIndex(inscmd));
364 } else if (cmdName == "include") {
365 inset.reset(new InsetInclude(inscmd));
366 } else if (cmdName == "label") {
367 inset.reset(new InsetLabel(inscmd));
368 } else if (cmdName == "url"
369 || cmdName == "htmlurl") {
370 inset.reset(new InsetUrl(inscmd));
371 } else if (cmdName == "ref"
372 || cmdName == "eqref"
373 || cmdName == "pageref"
375 || cmdName == "vpageref"
376 || cmdName == "prettyref") {
377 if (!inscmd.getOptions().empty()
378 || !inscmd.getContents().empty()) {
379 inset.reset(new InsetRef(inscmd, buf));
381 } else if (cmdName == "tableofcontents") {
382 inset.reset(new InsetTOC(inscmd));
383 } else if (cmdName == "listofalgorithms") {
384 inset.reset(new InsetFloatList("algorithm"));
385 } else if (cmdName == "listoffigures") {
386 inset.reset(new InsetFloatList("figure"));
387 } else if (cmdName == "listoftables") {
388 inset.reset(new InsetFloatList("table"));
389 } else if (cmdName == "printindex") {
390 inset.reset(new InsetPrintIndex(inscmd));
392 lyxerr << "unknown CommandInset '" << cmdName
394 while (lex.isOK() && lex.getString() != "\\end_inset")
399 if (tmptok == "Quotes") {
400 inset.reset(new InsetQuotes);
401 } else if (tmptok == "External") {
402 inset.reset(new InsetExternal);
403 } else if (tmptok == "FormulaMacro") {
404 inset.reset(new InsetFormulaMacro);
405 } else if (tmptok == "Formula") {
406 inset.reset(new InsetFormula);
407 } else if (tmptok == "Graphics") {
408 inset.reset(new InsetGraphics);
409 } else if (tmptok == "Note") {
410 inset.reset(new InsetNote(buf.params(), tmptok));
411 } else if (tmptok == "Box") {
412 inset.reset(new InsetBox(buf.params(), tmptok));
413 } else if (tmptok == "CharStyle") {
415 string s = lex.getString();
416 CharStyles::iterator found_cs = tclass.charstyle(s);
417 inset.reset(new InsetCharStyle(buf.params(), found_cs));
418 } else if (tmptok == "Branch") {
419 inset.reset(new InsetBranch(buf.params(),
420 InsetBranchParams()));
421 } else if (tmptok == "Include") {
422 InsetCommandParams p("Include");
423 inset.reset(new InsetInclude(p));
424 } else if (tmptok == "Environment") {
426 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
427 } else if (tmptok == "ERT") {
428 inset.reset(new InsetERT(buf.params()));
429 } else if (tmptok == "InsetSpace") {
430 inset.reset(new InsetSpace);
431 } else if (tmptok == "Tabular") {
432 inset.reset(new InsetTabular(buf));
433 } else if (tmptok == "Text") {
434 inset.reset(new InsetText(buf.params()));
435 } else if (tmptok == "VSpace") {
436 inset.reset(new InsetVSpace);
437 } else if (tmptok == "Foot") {
438 inset.reset(new InsetFoot(buf.params()));
439 } else if (tmptok == "Marginal") {
440 inset.reset(new InsetMarginal(buf.params()));
441 } else if (tmptok == "OptArg") {
442 inset.reset(new InsetOptArg(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();