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 "BufferParams.h"
17 #include "FloatList.h"
18 #include "FuncRequest.h"
21 #include "TextClass.h"
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetBox.h"
26 #include "insets/InsetBranch.h"
27 #include "insets/InsetCaption.h"
28 #include "insets/InsetCitation.h"
29 #include "insets/InsetFlex.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetListings.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/InsetHyperlink.h"
38 #include "insets/InsetInclude.h"
39 #include "insets/InsetIndex.h"
40 #include "insets/InsetInfo.h"
41 #include "insets/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.h"
44 #include "insets/InsetNewline.h"
45 #include "insets/InsetNewpage.h"
46 #include "insets/InsetNomencl.h"
47 #include "insets/InsetNote.h"
48 #include "insets/InsetArgument.h"
49 #include "insets/InsetPhantom.h"
50 #include "insets/InsetPreview.h"
51 #include "insets/InsetRef.h"
52 #include "insets/InsetScript.h"
53 #include "insets/InsetSpace.h"
54 #include "insets/InsetTabular.h"
55 #include "insets/InsetTOC.h"
56 #include "insets/InsetVSpace.h"
57 #include "insets/InsetWrap.h"
59 #include "mathed/MathMacroTemplate.h"
60 #include "mathed/InsetMathHull.h"
62 #include "frontends/alert.h"
64 #include "support/debug.h"
65 #include "support/lstrings.h"
66 #include "support/ExceptionMessage.h"
68 #include "support/lassert.h"
73 using namespace lyx::support;
77 namespace Alert = frontend::Alert;
80 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
84 switch (cmd.action()) {
86 case LFUN_NEWPAGE_INSERT: {
87 string const name = cmd.getArg(0);
88 InsetNewpageParams inp;
89 if (name.empty() || name == "newpage")
90 inp.kind = InsetNewpageParams::NEWPAGE;
91 else if (name == "pagebreak")
92 inp.kind = InsetNewpageParams::PAGEBREAK;
93 else if (name == "clearpage")
94 inp.kind = InsetNewpageParams::CLEARPAGE;
95 else if (name == "cleardoublepage")
96 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
97 return new InsetNewpage(inp);
100 case LFUN_FLEX_INSERT: {
101 string s = cmd.getArg(0);
102 return new InsetFlex(buf, s);
105 case LFUN_NOTE_INSERT: {
106 string arg = cmd.getArg(0);
109 return new InsetNote(buf, arg);
112 case LFUN_BOX_INSERT: {
113 string arg = cmd.getArg(0);
116 return new InsetBox(buf, arg);
119 case LFUN_BRANCH_INSERT: {
120 docstring arg = cmd.argument();
122 arg = from_ascii("none");
123 return new InsetBranch(buf, InsetBranchParams(arg));
126 case LFUN_PHANTOM_INSERT: {
127 string arg = cmd.getArg(0);
130 return new InsetPhantom(buf, arg);
133 case LFUN_ERT_INSERT:
134 return new InsetERT(buf);
136 case LFUN_LISTING_INSERT:
137 return new InsetListings(buf);
139 case LFUN_FOOTNOTE_INSERT:
140 return new InsetFoot(buf);
142 case LFUN_MARGINALNOTE_INSERT:
143 return new InsetMarginal(buf);
145 case LFUN_ARGUMENT_INSERT:
146 return new InsetArgument(buf);
148 case LFUN_FLOAT_INSERT:
149 return new InsetFloat(buf, to_utf8(cmd.argument()));
151 case LFUN_FLOAT_WIDE_INSERT: {
152 InsetFloat * fl = new InsetFloat(buf, to_utf8(cmd.argument()));
157 case LFUN_WRAP_INSERT: {
158 string const argument = to_utf8(cmd.argument());
159 if (argument == "figure" || argument == "table")
160 return new InsetWrap(buf, argument);
161 lyxerr << "Non-existent wrapfig type: " << argument << endl;
165 case LFUN_INDEX_INSERT: {
166 docstring arg = cmd.argument();
167 return new InsetIndex(buf, InsetIndexParams(arg));
170 case LFUN_NOMENCL_INSERT: {
171 InsetCommandParams icp(NOMENCL_CODE);
172 icp["symbol"] = cmd.argument();
173 return new InsetNomencl(buf, icp);
176 case LFUN_TABULAR_INSERT: {
177 if (cmd.argument().empty())
179 istringstream ss(to_utf8(cmd.argument()));
186 return new InsetTabular(buf, r, c);
189 case LFUN_CAPTION_INSERT:
190 return new InsetCaption(buf);
192 case LFUN_INDEX_PRINT: {
193 InsetCommandParams icp(INDEX_PRINT_CODE);
194 icp["type"] = cmd.argument();
195 return new InsetPrintIndex(buf, icp);
198 case LFUN_NOMENCL_PRINT: {
199 InsetCommandParams icp(NOMENCL_PRINT_CODE);
200 icp["set_width"] = from_ascii("auto");
201 return new InsetPrintNomencl(buf, icp);
204 case LFUN_INFO_INSERT: {
205 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
210 case LFUN_PREVIEW_INSERT:
211 return new InsetPreview(buf);
213 case LFUN_INSET_INSERT: {
214 string const name = cmd.getArg(0);
215 InsetCode code = insetCode(name);
218 lyxerr << "No such inset '" << name << "'.";
222 InsetCommandParams icp(code);
223 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
224 return new InsetBibitem(buf, icp);
228 InsetCommandParams icp(code);
229 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
230 return new InsetBibtex(buf, icp);
234 InsetCommandParams icp(code);
235 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
236 return new InsetCitation(buf, icp);
240 return new InsetERT(buf,
241 InsetERT::string2params(to_utf8(cmd.argument())));
244 case EXTERNAL_CODE: {
245 InsetExternalParams iep;
246 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
247 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
248 inset->setBuffer(*buf);
249 inset->setParams(iep);
250 return inset.release();
253 case GRAPHICS_CODE: {
254 InsetGraphicsParams igp;
255 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
256 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
257 inset->setParams(igp);
258 return inset.release();
261 case HYPERLINK_CODE: {
262 InsetCommandParams icp(code);
263 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
264 return new InsetHyperlink(buf, icp);
268 InsetCommandParams icp(code);
269 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
270 return new InsetInclude(buf, icp);
274 docstring arg = cmd.argument();
275 return new InsetIndex(buf, InsetIndexParams(arg));
278 case INDEX_PRINT_CODE: {
279 InsetCommandParams icp(code);
280 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
281 return new InsetPrintIndex(buf, icp);
285 InsetCommandParams icp(code);
286 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
287 return new InsetLabel(buf, icp);
291 InsetCommandParams icp(code);
292 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
293 return new InsetLine(buf, icp);
296 case LISTINGS_CODE: {
297 InsetListingsParams par;
298 InsetListings::string2params(to_utf8(cmd.argument()), par);
299 return new InsetListings(buf, par);
303 InsetCommandParams icp(code);
304 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
305 return new InsetNomencl(buf, icp);
309 InsetCommandParams icp(code);
310 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
311 return new InsetRef(buf, icp);
315 InsetScriptParams isp;
316 InsetScript::string2params(to_utf8(cmd.argument()), isp);
317 return new InsetScript(buf, isp);
321 InsetSpaceParams isp;
322 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
323 return new InsetSpace(isp);
327 InsetCommandParams icp(code);
328 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
329 return new InsetTOC(buf, icp);
334 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
335 return new InsetVSpace(vspace);
339 return new InsetPreview(buf);
342 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
347 } //end LFUN_INSET_INSERT
349 case LFUN_SPACE_INSERT: {
350 string const name = cmd.getArg(0);
351 string const len = cmd.getArg(1);
353 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
356 InsetSpaceParams isp;
357 // The tests for isp.math might be disabled after a file format change
358 if (name == "normal")
359 isp.kind = InsetSpaceParams::NORMAL;
360 else if (name == "protected")
361 isp.kind = InsetSpaceParams::PROTECTED;
362 else if (name == "thin")
363 isp.kind = InsetSpaceParams::THIN;
364 else if (isp.math && name == "med")
365 isp.kind = InsetSpaceParams::MEDIUM;
366 else if (isp.math && name == "thick")
367 isp.kind = InsetSpaceParams::THICK;
368 else if (name == "quad")
369 isp.kind = InsetSpaceParams::QUAD;
370 else if (name == "qquad")
371 isp.kind = InsetSpaceParams::QQUAD;
372 else if (name == "enspace")
373 isp.kind = InsetSpaceParams::ENSPACE;
374 else if (name == "enskip")
375 isp.kind = InsetSpaceParams::ENSKIP;
376 else if (name == "negthinspace")
377 isp.kind = InsetSpaceParams::NEGTHIN;
378 else if (isp.math && name == "negmedspace")
379 isp.kind = InsetSpaceParams::NEGMEDIUM;
380 else if (isp.math && name == "negthickspace")
381 isp.kind = InsetSpaceParams::NEGTHICK;
382 else if (name == "hfill")
383 isp.kind = InsetSpaceParams::HFILL;
384 else if (name == "hfill*")
385 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
386 else if (name == "dotfill")
387 isp.kind = InsetSpaceParams::DOTFILL;
388 else if (name == "hrulefill")
389 isp.kind = InsetSpaceParams::HRULEFILL;
390 else if (name == "hspace") {
391 if (len.empty() || !isValidGlueLength(len)) {
392 lyxerr << "LyX function 'space-insert hspace' "
393 << "needs a valid length argument." << endl;
396 isp.kind = InsetSpaceParams::CUSTOM;
397 isp.length = GlueLength(len);
399 else if (name == "hspace*") {
400 if (len.empty() || !isValidGlueLength(len)) {
401 lyxerr << "LyX function 'space-insert hspace*' "
402 << "needs a valid length argument." << endl;
405 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
406 isp.length = GlueLength(len);
409 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
412 return new InsetSpace(isp);
420 } catch (ExceptionMessage const & message) {
421 if (message.type_ == ErrorException) {
422 // This should never happen!
423 Alert::error(message.title_, message.details_);
425 } else if (message.type_ == WarningException) {
426 Alert::warning(message.title_, message.details_);
435 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
437 Inset * inset = createInsetHelper(buf, cmd);
439 inset->setBuffer(*buf);
444 Inset * readInset(Lexer & lex, Buffer * buf)
447 if (lex.getString() != "\\begin_inset")
448 LYXERR0("Buffer::readInset: Consistency check failed.");
450 auto_ptr<Inset> inset;
455 // test the different insets
457 // FIXME It would be better if we did not have this branch and could
458 // just do one massive switch for all insets. But at present, it's
459 // easier to do it this way, and we can't do the massive switch until
460 // the conversion mentioned below. Note that if we do want to do a
461 // single switch, we need to remove this "CommandInset" line---or
462 // replace it with a single "InsetType" line that would be used in all
464 if (tmptok == "CommandInset") {
466 string const insetType = lex.getString();
467 lex.pushToken(insetType);
469 InsetCode const code = insetCode(insetType);
471 //FIXME If we do the one massive switch, we cannot do this here, since
472 //we do not know in advance that we're dealing with a command inset.
473 //Worst case, we could put it in each case below. Better, we could
474 //pass the lexer to the constructor and let the params be built there.
475 InsetCommandParams inscmd(code);
480 inset.reset(new InsetBibitem(buf, inscmd));
483 inset.reset(new InsetBibtex(buf, inscmd));
486 inset.reset(new InsetCitation(buf, inscmd));
489 inset.reset(new InsetHyperlink(buf, inscmd));
492 inset.reset(new InsetInclude(buf, inscmd));
494 case INDEX_PRINT_CODE:
495 inset.reset(new InsetPrintIndex(buf, inscmd));
498 inset.reset(new InsetLabel(buf, inscmd));
501 inset.reset(new InsetLine(buf, inscmd));
504 inset.reset(new InsetNomencl(buf, inscmd));
506 case NOMENCL_PRINT_CODE:
507 inset.reset(new InsetPrintNomencl(buf, inscmd));
510 if (inscmd["name"].empty() && inscmd["reference"].empty())
512 inset.reset(new InsetRef(buf, inscmd));
515 inset.reset(new InsetTOC(buf, inscmd));
519 lyxerr << "unknown CommandInset '" << insetType
521 while (lex.isOK() && lex.getString() != "\\end_inset")
525 inset->setBuffer(*buf);
527 // FIXME This branch should be made to use inset codes as the preceding
528 // branch does. Unfortunately, that will take some doing. It requires
529 // converting the representation of the insets in LyX files so that they
530 // use the inset names listed in Inset.cpp. Then, as above, the inset names
531 // can be translated to inset codes using insetCode(). And the insets'
532 // write() routines should use insetName() rather than hardcoding it.
533 if (tmptok == "Quotes") {
534 inset.reset(new InsetQuotes(buf));
535 } else if (tmptok == "External") {
536 inset.reset(new InsetExternal(buf));
537 } else if (tmptok == "FormulaMacro") {
538 inset.reset(new MathMacroTemplate(buf));
539 } else if (tmptok == "Formula") {
540 inset.reset(new InsetMathHull(buf));
541 } else if (tmptok == "Graphics") {
542 inset.reset(new InsetGraphics(buf));
543 } else if (tmptok == "Note") {
544 inset.reset(new InsetNote(buf, tmptok));
545 } else if (tmptok == "Box") {
546 inset.reset(new InsetBox(buf, tmptok));
547 } else if (tmptok == "Flex") {
549 string s = lex.getString();
550 inset.reset(new InsetFlex(buf, s));
551 } else if (tmptok == "Branch") {
552 inset.reset(new InsetBranch(buf, InsetBranchParams()));
553 } else if (tmptok == "Phantom") {
554 inset.reset(new InsetPhantom(buf, tmptok));
555 } else if (tmptok == "ERT") {
556 inset.reset(new InsetERT(buf));
557 } else if (tmptok == "listings") {
558 inset.reset(new InsetListings(buf));
559 } else if (tmptok == "script") {
560 inset.reset(new InsetScript(buf));
561 } else if (tmptok == "space") {
562 inset.reset(new InsetSpace);
563 } else if (tmptok == "Tabular") {
564 inset.reset(new InsetTabular(buf));
565 } else if (tmptok == "Text") {
566 inset.reset(new InsetText(buf));
567 } else if (tmptok == "VSpace") {
568 inset.reset(new InsetVSpace);
569 } else if (tmptok == "Foot") {
570 inset.reset(new InsetFoot(buf));
571 } else if (tmptok == "Marginal") {
572 inset.reset(new InsetMarginal(buf));
573 } else if (tmptok == "Newpage") {
574 inset.reset(new InsetNewpage);
575 } else if (tmptok == "Newline") {
576 inset.reset(new InsetNewline);
577 } else if (tmptok == "Argument") {
578 inset.reset(new InsetArgument(buf));
579 } else if (tmptok == "Float") {
580 inset.reset(new InsetFloat(buf, string()));
581 } else if (tmptok == "Wrap") {
583 string tmptok = lex.getString();
584 inset.reset(new InsetWrap(buf, tmptok));
585 } else if (tmptok == "Caption") {
586 inset.reset(new InsetCaption(buf));
587 } else if (tmptok == "Index") {
588 inset.reset(new InsetIndex(buf, InsetIndexParams()));
589 } else if (tmptok == "FloatList") {
590 inset.reset(new InsetFloatList(buf));
591 } else if (tmptok == "Info") {
592 inset.reset(new InsetInfo(buf));
593 } else if (tmptok == "Preview") {
594 inset.reset(new InsetPreview(buf));
596 lyxerr << "unknown Inset type '" << tmptok
598 while (lex.isOK() && lex.getString() != "\\end_inset")
603 // Set the buffer reference for proper parsing of some insets
604 // (InsetCollapsable for example)
605 inset->setBuffer(*buf);
607 // Set again the buffer for insets that are created inside this inset
608 // (InsetMathHull for example).
609 inset->setBuffer(*buf);
611 return inset.release();