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/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_INSET_MINIPAGE:
88 return new InsetMinipage(params);
90 case LFUN_INSERT_CHARSTYLE: {
91 string s = cmd.getArg(0);
92 CharStyles::iterator found_cs = params.getLyXTextClass().charstyle(s);
93 return new InsetCharStyle(params, found_cs);
96 case LFUN_INSERT_NOTE: {
97 string arg = cmd.getArg(0);
100 return new InsetNote(params, arg);
103 case LFUN_INSERT_BOX: {
104 string arg = cmd.getArg(0);
107 return new InsetBox(params, arg);
110 case LFUN_INSERT_BRANCH: {
111 string arg = cmd.getArg(0);
114 return new InsetBranch(params, arg);
118 return new InsetERT(params);
120 case LFUN_INSET_FOOTNOTE:
121 return new InsetFoot(params);
123 case LFUN_INSET_MARGINAL:
124 return new InsetMarginal(params);
126 case LFUN_INSET_OPTARG:
127 return new InsetOptArg(params);
129 case LFUN_INSERT_BIBITEM:
130 return new InsetBibitem(InsetCommandParams("bibitem"));
132 case LFUN_INSET_FLOAT:
133 // check if the float type exists
134 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
135 return new InsetFloat(params, cmd.argument);
136 lyxerr << "Non-existent float type: " << cmd.argument << endl;
139 case LFUN_INSET_WIDE_FLOAT:
140 // check if the float type exists
141 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
142 auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
143 p->wide(true, params);
146 lyxerr << "Non-existent float type: " << cmd.argument << endl;
149 case LFUN_INSET_WRAP:
150 if (cmd.argument == "figure")
151 return new InsetWrap(params, cmd.argument);
152 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
155 case LFUN_INDEX_INSERT: {
156 // Try and generate a valid index entry.
157 InsetCommandParams icp("index");
158 string const contents = cmd.argument.empty() ?
159 bv->getLyXText()->getStringToIndex() :
161 icp.setContents(contents);
163 string data = InsetCommandMailer::params2string("index", icp);
164 LyXView * lv = bv->owner();
166 if (icp.getContents().empty()) {
167 lv->getDialogs().show("index", data, 0);
169 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
174 case LFUN_TABULAR_INSERT:
175 if (!cmd.argument.empty()) {
176 std::istringstream ss(cmd.argument);
181 return new InsetTabular(*bv->buffer(), r, c);
183 bv->owner()->getDialogs().show("tabularcreate");
186 case LFUN_INSET_CAPTION:
187 if (bv->innerInset()) {
188 auto_ptr<InsetCaption> inset(new InsetCaption(params));
189 inset->setOwner(bv->innerInset());
190 inset->setAutoBreakRows(true);
191 inset->setDrawFrame(InsetText::LOCKED);
192 inset->setFrameColor(LColor::captionframe);
193 return inset.release();
196 case LFUN_INDEX_PRINT:
197 return new InsetPrintIndex(InsetCommandParams("printindex"));
199 case LFUN_TOC_INSERT:
200 return new InsetTOC(InsetCommandParams("tableofcontents"));
202 case LFUN_ENVIRONMENT_INSERT:
203 return new InsetEnvironment(params, cmd.argument);
206 case LFUN_INSET_LIST:
207 return new InsetList;
209 case LFUN_INSET_THEOREM:
210 return new InsetTheorem;
213 case LFUN_INSET_INSERT: {
214 string const name = cmd.getArg(0);
216 if (name == "bibitem") {
217 InsetCommandParams icp;
218 InsetCommandMailer::string2params(cmd.argument, icp);
219 return new InsetBibitem(icp);
221 } else if (name == "bibtex") {
222 InsetCommandParams icp;
223 InsetCommandMailer::string2params(cmd.argument, icp);
224 return new InsetBibtex(icp);
226 } else if (name == "citation") {
227 InsetCommandParams icp;
228 InsetCommandMailer::string2params(cmd.argument, icp);
229 return new InsetCitation(icp);
231 } else if (name == "ert") {
232 InsetERT::ERTStatus s;
233 InsetERTMailer::string2params(cmd.argument, s);
234 return new InsetERT(params, s);
236 } else if (name == "external") {
237 Buffer const & buffer = *cmd.view()->buffer();
238 InsetExternalParams iep;
239 InsetExternalMailer::string2params(cmd.argument,
241 auto_ptr<InsetExternal> inset(new InsetExternal);
242 inset->setParams(iep, buffer);
243 return inset.release();
245 } else if (name == "graphics") {
246 Buffer const & buffer = *cmd.view()->buffer();
247 InsetGraphicsParams igp;
248 InsetGraphicsMailer::string2params(cmd.argument,
250 auto_ptr<InsetGraphics> inset(new InsetGraphics);
251 inset->setParams(igp);
252 return inset.release();
254 } else if (name == "include") {
255 InsetCommandParams iip;
256 InsetIncludeMailer::string2params(cmd.argument, iip);
257 return new InsetInclude(iip);
259 } else if (name == "index") {
260 InsetCommandParams icp;
261 InsetCommandMailer::string2params(cmd.argument, icp);
262 return new InsetIndex(icp);
264 } else if (name == "label") {
265 InsetCommandParams icp;
266 InsetCommandMailer::string2params(cmd.argument, icp);
267 return new InsetLabel(icp);
269 } else if (name == "ref") {
270 InsetCommandParams icp;
271 InsetCommandMailer::string2params(cmd.argument, icp);
272 return new InsetRef(icp, *bv->buffer());
274 } else if (name == "toc") {
275 InsetCommandParams icp;
276 InsetCommandMailer::string2params(cmd.argument, icp);
277 return new InsetTOC(icp);
279 } else if (name == "url") {
280 InsetCommandParams icp;
281 InsetCommandMailer::string2params(cmd.argument, icp);
282 return new InsetUrl(icp);
286 case LFUN_SPACE_INSERT: {
287 string const name = cmd.argument;
288 if (name == "normal")
289 return new InsetSpace(InsetSpace::NORMAL);
290 else if (name == "protected")
291 return new InsetSpace(InsetSpace::PROTECTED);
292 else if (name == "thin")
293 return new InsetSpace(InsetSpace::THIN);
294 else if (name == "quad")
295 return new InsetSpace(InsetSpace::QUAD);
296 else if (name == "qquad")
297 return new InsetSpace(InsetSpace::QQUAD);
298 else if (name == "enspace")
299 return new InsetSpace(InsetSpace::ENSPACE);
300 else if (name == "enskip")
301 return new InsetSpace(InsetSpace::ENSKIP);
302 else if (name == "negthinspace")
303 return new InsetSpace(InsetSpace::NEGTHIN);
304 else if (name.empty())
305 lyxerr << "LyX function 'space' needs an argument." << endl;
307 lyxerr << "Wrong argument for LyX function 'space'." << endl;
320 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
323 if (lex.getString() != "\\begin_inset") {
324 lyxerr << "Buffer::readInset: Consistency check failed."
328 auto_ptr<InsetOld> inset;
330 LyXTextClass tclass = buf.params().getLyXTextClass();
333 string tmptok = lex.getString();
334 CharStyles::iterator found_cs = tclass.charstyle(tmptok);
335 if (found_cs != tclass.charstyles().end())
336 tmptok = "CharStyle";
338 // test the different insets
339 if (tmptok == "LatexCommand") {
340 InsetCommandParams inscmd;
343 string const cmdName = inscmd.getCmdName();
345 // This strange command allows LyX to recognize "natbib" style
346 // citations: citet, citep, Citet etc.
347 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
348 inset.reset(new InsetCitation(inscmd));
349 } else if (cmdName == "bibitem") {
350 lex.printError("Wrong place for bibitem");
351 inset.reset(new InsetBibitem(inscmd));
352 } else if (cmdName == "bibtex") {
353 inset.reset(new InsetBibtex(inscmd));
354 } else if (cmdName == "index") {
355 inset.reset(new InsetIndex(inscmd));
356 } else if (cmdName == "include") {
357 inset.reset(new InsetInclude(inscmd));
358 } else if (cmdName == "label") {
359 inset.reset(new InsetLabel(inscmd));
360 } else if (cmdName == "url"
361 || cmdName == "htmlurl") {
362 inset.reset(new InsetUrl(inscmd));
363 } else if (cmdName == "ref"
364 || cmdName == "eqref"
365 || cmdName == "pageref"
367 || cmdName == "vpageref"
368 || cmdName == "prettyref") {
369 if (!inscmd.getOptions().empty()
370 || !inscmd.getContents().empty()) {
371 inset.reset(new InsetRef(inscmd, buf));
373 } else if (cmdName == "tableofcontents") {
374 inset.reset(new InsetTOC(inscmd));
375 } else if (cmdName == "listofalgorithms") {
376 inset.reset(new InsetFloatList("algorithm"));
377 } else if (cmdName == "listoffigures") {
378 inset.reset(new InsetFloatList("figure"));
379 } else if (cmdName == "listoftables") {
380 inset.reset(new InsetFloatList("table"));
381 } else if (cmdName == "printindex") {
382 inset.reset(new InsetPrintIndex(inscmd));
384 lyxerr << "unknown CommandInset '" << cmdName
386 while (lex.isOK() && lex.getString() != "\\end_inset")
391 if (tmptok == "Quotes") {
392 inset.reset(new InsetQuotes);
393 } else if (tmptok == "External") {
394 inset.reset(new InsetExternal);
395 } else if (tmptok == "FormulaMacro") {
396 inset.reset(new InsetFormulaMacro);
397 } else if (tmptok == "Formula") {
398 inset.reset(new InsetFormula);
399 } else if (tmptok == "Graphics") {
400 inset.reset(new InsetGraphics);
401 } else if (tmptok == "Note" || tmptok == "Comment"
402 || tmptok == "Greyedout") {
403 inset.reset(new InsetNote(buf.params(), tmptok));
404 } else if (tmptok == "Boxed" || tmptok == "ovalbox"
405 || tmptok == "Shadowbox" || tmptok == "Doublebox"
406 || tmptok == "Ovalbox" || tmptok == "Frameless") {
407 inset.reset(new InsetBox(buf.params(), tmptok));
408 } else if (tmptok == "CharStyle") {
409 inset.reset(new InsetCharStyle(buf.params(), found_cs));
410 } else if (tmptok == "Branch") {
411 inset.reset(new InsetBranch(buf.params(), string()));
412 } else if (tmptok == "Include") {
413 InsetCommandParams p("Include");
414 inset.reset(new InsetInclude(p));
415 } else if (tmptok == "Environment") {
417 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
418 } else if (tmptok == "ERT") {
419 inset.reset(new InsetERT(buf.params()));
420 } else if (tmptok == "InsetSpace") {
421 inset.reset(new InsetSpace);
422 } else if (tmptok == "Tabular") {
423 inset.reset(new InsetTabular(buf));
424 } else if (tmptok == "Text") {
425 inset.reset(new InsetText(buf.params()));
426 } else if (tmptok == "Foot") {
427 inset.reset(new InsetFoot(buf.params()));
428 } else if (tmptok == "Marginal") {
429 inset.reset(new InsetMarginal(buf.params()));
430 } else if (tmptok == "OptArg") {
431 inset.reset(new InsetOptArg(buf.params()));
432 } else if (tmptok == "Minipage") {
433 inset.reset(new InsetMinipage(buf.params()));
434 } else if (tmptok == "Float") {
436 string tmptok = lex.getString();
437 inset.reset(new InsetFloat(buf.params(), tmptok));
438 } else if (tmptok == "Wrap") {
440 string tmptok = lex.getString();
441 inset.reset(new InsetWrap(buf.params(), tmptok));
443 } else if (tmptok == "List") {
444 inset.reset(new InsetList);
445 } else if (tmptok == "Theorem") {
446 inset.reset(new InsetList);
448 } else if (tmptok == "Caption") {
449 inset.reset(new InsetCaption(buf.params()));
450 } else if (tmptok == "FloatList") {
451 inset.reset(new InsetFloatList);
453 lyxerr << "unknown Inset type '" << tmptok
455 while (lex.isOK() && lex.getString() != "\\end_inset")
460 inset->read(buf, lex);
463 return inset.release();