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/InsetSpace.h"
53 #include "insets/InsetTabular.h"
54 #include "insets/InsetTOC.h"
55 #include "insets/InsetVSpace.h"
56 #include "insets/InsetWrap.h"
58 #include "mathed/MathMacroTemplate.h"
59 #include "mathed/InsetMathHull.h"
61 #include "frontends/alert.h"
63 #include "support/debug.h"
64 #include "support/lstrings.h"
65 #include "support/ExceptionMessage.h"
67 #include "support/lassert.h"
72 using namespace lyx::support;
76 namespace Alert = frontend::Alert;
79 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
83 switch (cmd.action()) {
85 case LFUN_NEWPAGE_INSERT: {
86 string const name = cmd.getArg(0);
87 InsetNewpageParams inp;
88 if (name.empty() || name == "newpage")
89 inp.kind = InsetNewpageParams::NEWPAGE;
90 else if (name == "pagebreak")
91 inp.kind = InsetNewpageParams::PAGEBREAK;
92 else if (name == "clearpage")
93 inp.kind = InsetNewpageParams::CLEARPAGE;
94 else if (name == "cleardoublepage")
95 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
96 return new InsetNewpage(inp);
99 case LFUN_FLEX_INSERT: {
100 string s = cmd.getArg(0);
101 return new InsetFlex(buf, s);
104 case LFUN_NOTE_INSERT: {
105 string arg = cmd.getArg(0);
108 return new InsetNote(buf, arg);
111 case LFUN_BOX_INSERT: {
112 string arg = cmd.getArg(0);
115 return new InsetBox(buf, arg);
118 case LFUN_BRANCH_INSERT: {
119 docstring arg = cmd.argument();
121 arg = from_ascii("none");
122 return new InsetBranch(buf, InsetBranchParams(arg));
125 case LFUN_PHANTOM_INSERT: {
126 string arg = cmd.getArg(0);
129 return new InsetPhantom(buf, arg);
132 case LFUN_ERT_INSERT:
133 return new InsetERT(buf);
135 case LFUN_LISTING_INSERT:
136 return new InsetListings(buf);
138 case LFUN_FOOTNOTE_INSERT:
139 return new InsetFoot(buf);
141 case LFUN_MARGINALNOTE_INSERT:
142 return new InsetMarginal(buf);
144 case LFUN_ARGUMENT_INSERT:
145 return new InsetArgument(buf);
147 case LFUN_FLOAT_INSERT:
148 return new InsetFloat(buf, to_utf8(cmd.argument()));
150 case LFUN_FLOAT_WIDE_INSERT: {
151 InsetFloat * fl = new InsetFloat(buf, to_utf8(cmd.argument()));
156 case LFUN_WRAP_INSERT: {
157 string const argument = to_utf8(cmd.argument());
158 if (argument == "figure" || argument == "table")
159 return new InsetWrap(buf, argument);
160 lyxerr << "Non-existent wrapfig type: " << argument << endl;
164 case LFUN_INDEX_INSERT: {
165 docstring arg = cmd.argument();
166 return new InsetIndex(buf, InsetIndexParams(arg));
169 case LFUN_NOMENCL_INSERT: {
170 InsetCommandParams icp(NOMENCL_CODE);
171 icp["symbol"] = cmd.argument();
172 return new InsetNomencl(buf, icp);
175 case LFUN_TABULAR_INSERT: {
176 if (cmd.argument().empty())
178 istringstream ss(to_utf8(cmd.argument()));
185 return new InsetTabular(buf, r, c);
188 case LFUN_CAPTION_INSERT:
189 return new InsetCaption(buf);
191 case LFUN_INDEX_PRINT: {
192 InsetCommandParams icp(INDEX_PRINT_CODE);
193 icp["type"] = cmd.argument();
194 return new InsetPrintIndex(buf, icp);
197 case LFUN_NOMENCL_PRINT: {
198 InsetCommandParams icp(NOMENCL_PRINT_CODE);
199 icp["set_width"] = from_ascii("auto");
200 return new InsetPrintNomencl(buf, icp);
203 case LFUN_TOC_INSERT:
204 return new InsetTOC(buf, InsetCommandParams(TOC_CODE));
206 case LFUN_INFO_INSERT: {
207 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
212 case LFUN_PREVIEW_INSERT:
213 return new InsetPreview(buf);
215 case LFUN_INSET_INSERT: {
216 string const name = cmd.getArg(0);
217 InsetCode code = insetCode(name);
220 lyxerr << "No such inset '" << name << "'.";
224 InsetCommandParams icp(code);
225 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
226 return new InsetBibitem(buf, icp);
230 InsetCommandParams icp(code);
231 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
232 return new InsetBibtex(buf, icp);
236 InsetCommandParams icp(code);
237 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
238 return new InsetCitation(buf, icp);
242 return new InsetERT(buf,
243 InsetERT::string2params(to_utf8(cmd.argument())));
246 case EXTERNAL_CODE: {
247 InsetExternalParams iep;
248 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
249 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
250 inset->setBuffer(*buf);
251 inset->setParams(iep);
252 return inset.release();
255 case GRAPHICS_CODE: {
256 InsetGraphicsParams igp;
257 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
258 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
259 inset->setParams(igp);
260 return inset.release();
263 case HYPERLINK_CODE: {
264 InsetCommandParams icp(code);
265 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
266 return new InsetHyperlink(buf, icp);
270 InsetCommandParams icp(code);
271 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
272 return new InsetInclude(buf, icp);
276 docstring arg = cmd.argument();
277 return new InsetIndex(buf, InsetIndexParams(arg));
280 case INDEX_PRINT_CODE: {
281 InsetCommandParams icp(code);
282 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
283 return new InsetPrintIndex(buf, icp);
287 InsetCommandParams icp(code);
288 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
289 return new InsetLabel(buf, icp);
293 InsetCommandParams icp(code);
294 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
295 return new InsetLine(buf, icp);
298 case LISTINGS_CODE: {
299 InsetListingsParams par;
300 InsetListings::string2params(to_utf8(cmd.argument()), par);
301 return new InsetListings(buf, par);
305 InsetCommandParams icp(code);
306 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
307 return new InsetNomencl(buf, icp);
311 InsetCommandParams icp(code);
312 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
313 return new InsetRef(buf, icp);
317 InsetSpaceParams isp;
318 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
319 return new InsetSpace(isp);
323 InsetCommandParams icp(code);
324 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
325 return new InsetTOC(buf, icp);
330 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
331 return new InsetVSpace(vspace);
335 return new InsetPreview(buf);
338 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
343 } //end LFUN_INSET_INSERT
345 case LFUN_SPACE_INSERT: {
346 string const name = cmd.getArg(0);
347 string const len = cmd.getArg(1);
349 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
352 InsetSpaceParams isp;
353 // The tests for isp.math might be disabled after a file format change
354 if (name == "normal")
355 isp.kind = InsetSpaceParams::NORMAL;
356 else if (name == "protected")
357 isp.kind = InsetSpaceParams::PROTECTED;
358 else if (name == "thin")
359 isp.kind = InsetSpaceParams::THIN;
360 else if (isp.math && name == "med")
361 isp.kind = InsetSpaceParams::MEDIUM;
362 else if (isp.math && name == "thick")
363 isp.kind = InsetSpaceParams::THICK;
364 else if (name == "quad")
365 isp.kind = InsetSpaceParams::QUAD;
366 else if (name == "qquad")
367 isp.kind = InsetSpaceParams::QQUAD;
368 else if (name == "enspace")
369 isp.kind = InsetSpaceParams::ENSPACE;
370 else if (name == "enskip")
371 isp.kind = InsetSpaceParams::ENSKIP;
372 else if (name == "negthinspace")
373 isp.kind = InsetSpaceParams::NEGTHIN;
374 else if (isp.math && name == "negmedspace")
375 isp.kind = InsetSpaceParams::NEGMEDIUM;
376 else if (isp.math && name == "negthickspace")
377 isp.kind = InsetSpaceParams::NEGTHICK;
378 else if (name == "hfill")
379 isp.kind = InsetSpaceParams::HFILL;
380 else if (name == "hfill*")
381 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
382 else if (name == "dotfill")
383 isp.kind = InsetSpaceParams::DOTFILL;
384 else if (name == "hrulefill")
385 isp.kind = InsetSpaceParams::HRULEFILL;
386 else if (name == "hspace") {
387 if (len.empty() || !isValidGlueLength(len)) {
388 lyxerr << "LyX function 'space-insert hspace' "
389 << "needs a valid length argument." << endl;
392 isp.kind = InsetSpaceParams::CUSTOM;
393 isp.length = GlueLength(len);
395 else if (name == "hspace*") {
396 if (len.empty() || !isValidGlueLength(len)) {
397 lyxerr << "LyX function 'space-insert hspace*' "
398 << "needs a valid length argument." << endl;
401 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
402 isp.length = GlueLength(len);
405 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
408 return new InsetSpace(isp);
416 } catch (ExceptionMessage const & message) {
417 if (message.type_ == ErrorException) {
418 // This should never happen!
419 Alert::error(message.title_, message.details_);
421 } else if (message.type_ == WarningException) {
422 Alert::warning(message.title_, message.details_);
431 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
433 Inset * inset = createInsetHelper(buf, cmd);
435 inset->setBuffer(*buf);
440 Inset * readInset(Lexer & lex, Buffer * buf)
443 if (lex.getString() != "\\begin_inset")
444 LYXERR0("Buffer::readInset: Consistency check failed.");
446 auto_ptr<Inset> inset;
451 // test the different insets
453 // FIXME It would be better if we did not have this branch and could
454 // just do one massive switch for all insets. But at present, it's
455 // easier to do it this way, and we can't do the massive switch until
456 // the conversion mentioned below. Note that if we do want to do a
457 // single switch, we need to remove this "CommandInset" line---or
458 // replace it with a single "InsetType" line that would be used in all
460 if (tmptok == "CommandInset") {
462 string const insetType = lex.getString();
463 lex.pushToken(insetType);
465 InsetCode const code = insetCode(insetType);
467 //FIXME If we do the one massive switch, we cannot do this here, since
468 //we do not know in advance that we're dealing with a command inset.
469 //Worst case, we could put it in each case below. Better, we could
470 //pass the lexer to the constructor and let the params be built there.
471 InsetCommandParams inscmd(code);
476 inset.reset(new InsetBibitem(buf, inscmd));
479 inset.reset(new InsetBibtex(buf, inscmd));
482 inset.reset(new InsetCitation(buf, inscmd));
485 inset.reset(new InsetHyperlink(buf, inscmd));
488 inset.reset(new InsetInclude(buf, inscmd));
490 case INDEX_PRINT_CODE:
491 inset.reset(new InsetPrintIndex(buf, inscmd));
494 inset.reset(new InsetLabel(buf, inscmd));
497 inset.reset(new InsetLine(buf, inscmd));
500 inset.reset(new InsetNomencl(buf, inscmd));
502 case NOMENCL_PRINT_CODE:
503 inset.reset(new InsetPrintNomencl(buf, inscmd));
506 if (inscmd["name"].empty() && inscmd["reference"].empty())
508 inset.reset(new InsetRef(buf, inscmd));
511 inset.reset(new InsetTOC(buf, inscmd));
515 lyxerr << "unknown CommandInset '" << insetType
517 while (lex.isOK() && lex.getString() != "\\end_inset")
521 inset->setBuffer(*buf);
523 // FIXME This branch should be made to use inset codes as the preceding
524 // branch does. Unfortunately, that will take some doing. It requires
525 // converting the representation of the insets in LyX files so that they
526 // use the inset names listed in Inset.cpp. Then, as above, the inset names
527 // can be translated to inset codes using insetCode(). And the insets'
528 // write() routines should use insetName() rather than hardcoding it.
529 if (tmptok == "Quotes") {
530 inset.reset(new InsetQuotes(buf));
531 } else if (tmptok == "External") {
532 inset.reset(new InsetExternal(buf));
533 } else if (tmptok == "FormulaMacro") {
534 inset.reset(new MathMacroTemplate(buf));
535 } else if (tmptok == "Formula") {
536 inset.reset(new InsetMathHull(buf));
537 } else if (tmptok == "Graphics") {
538 inset.reset(new InsetGraphics(buf));
539 } else if (tmptok == "Note") {
540 inset.reset(new InsetNote(buf, tmptok));
541 } else if (tmptok == "Box") {
542 inset.reset(new InsetBox(buf, tmptok));
543 } else if (tmptok == "Flex") {
545 string s = lex.getString();
546 inset.reset(new InsetFlex(buf, s));
547 } else if (tmptok == "Branch") {
548 inset.reset(new InsetBranch(buf, InsetBranchParams()));
549 } else if (tmptok == "Phantom") {
550 inset.reset(new InsetPhantom(buf, tmptok));
551 } else if (tmptok == "ERT") {
552 inset.reset(new InsetERT(buf));
553 } else if (tmptok == "listings") {
554 inset.reset(new InsetListings(buf));
555 } else if (tmptok == "space") {
556 inset.reset(new InsetSpace);
557 } else if (tmptok == "Tabular") {
558 inset.reset(new InsetTabular(buf));
559 } else if (tmptok == "Text") {
560 inset.reset(new InsetText(buf));
561 } else if (tmptok == "VSpace") {
562 inset.reset(new InsetVSpace);
563 } else if (tmptok == "Foot") {
564 inset.reset(new InsetFoot(buf));
565 } else if (tmptok == "Marginal") {
566 inset.reset(new InsetMarginal(buf));
567 } else if (tmptok == "Newpage") {
568 inset.reset(new InsetNewpage);
569 } else if (tmptok == "Newline") {
570 inset.reset(new InsetNewline);
571 } else if (tmptok == "Argument") {
572 inset.reset(new InsetArgument(buf));
573 } else if (tmptok == "Float") {
574 inset.reset(new InsetFloat(buf, string()));
575 } else if (tmptok == "Wrap") {
577 string tmptok = lex.getString();
578 inset.reset(new InsetWrap(buf, tmptok));
579 } else if (tmptok == "Caption") {
580 inset.reset(new InsetCaption(buf));
581 } else if (tmptok == "Index") {
582 inset.reset(new InsetIndex(buf, InsetIndexParams()));
583 } else if (tmptok == "FloatList") {
584 inset.reset(new InsetFloatList(buf));
585 } else if (tmptok == "Info") {
586 inset.reset(new InsetInfo(buf));
587 } else if (tmptok == "Preview") {
588 inset.reset(new InsetPreview(buf));
590 lyxerr << "unknown Inset type '" << tmptok
592 while (lex.isOK() && lex.getString() != "\\end_inset")
597 // Set the buffer reference for proper parsing of some insets
598 // (InsetCollapsable for example)
599 inset->setBuffer(*buf);
601 // Set again the buffer for insets that are created inside this inset
602 // (InsetMathHull for example).
603 inset->setBuffer(*buf);
605 return inset.release();