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_LINE_INSERT:
88 case LFUN_NEWPAGE_INSERT: {
89 string const name = cmd.getArg(0);
90 InsetNewpageParams inp;
91 if (name.empty() || name == "newpage")
92 inp.kind = InsetNewpageParams::NEWPAGE;
93 else if (name == "pagebreak")
94 inp.kind = InsetNewpageParams::PAGEBREAK;
95 else if (name == "clearpage")
96 inp.kind = InsetNewpageParams::CLEARPAGE;
97 else if (name == "cleardoublepage")
98 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
99 return new InsetNewpage(inp);
102 case LFUN_FLEX_INSERT: {
103 string s = cmd.getArg(0);
104 return new InsetFlex(buf, s);
107 case LFUN_NOTE_INSERT: {
108 string arg = cmd.getArg(0);
111 return new InsetNote(buf, arg);
114 case LFUN_BOX_INSERT: {
115 string arg = cmd.getArg(0);
118 return new InsetBox(buf, arg);
121 case LFUN_BRANCH_INSERT: {
122 docstring arg = cmd.argument();
124 arg = from_ascii("none");
125 return new InsetBranch(buf, InsetBranchParams(arg));
128 case LFUN_PHANTOM_INSERT: {
129 string arg = cmd.getArg(0);
132 return new InsetPhantom(buf, arg);
135 case LFUN_ERT_INSERT:
136 return new InsetERT(buf);
138 case LFUN_LISTING_INSERT:
139 return new InsetListings(buf);
141 case LFUN_FOOTNOTE_INSERT:
142 return new InsetFoot(buf);
144 case LFUN_MARGINALNOTE_INSERT:
145 return new InsetMarginal(buf);
147 case LFUN_ARGUMENT_INSERT:
148 return new InsetArgument(buf);
150 case LFUN_FLOAT_INSERT:
151 return new InsetFloat(buf, to_utf8(cmd.argument()));
153 case LFUN_FLOAT_WIDE_INSERT: {
154 InsetFloat * fl = new InsetFloat(buf, to_utf8(cmd.argument()));
159 case LFUN_WRAP_INSERT: {
160 string const argument = to_utf8(cmd.argument());
161 if (argument == "figure" || argument == "table")
162 return new InsetWrap(buf, argument);
163 lyxerr << "Non-existent wrapfig type: " << argument << endl;
167 case LFUN_INDEX_INSERT: {
168 docstring arg = cmd.argument();
169 return new InsetIndex(buf, InsetIndexParams(arg));
172 case LFUN_NOMENCL_INSERT: {
173 InsetCommandParams icp(NOMENCL_CODE);
174 icp["symbol"] = cmd.argument();
175 return new InsetNomencl(buf, icp);
178 case LFUN_TABULAR_INSERT: {
179 if (cmd.argument().empty())
181 istringstream ss(to_utf8(cmd.argument()));
188 return new InsetTabular(buf, r, c);
191 case LFUN_CAPTION_INSERT:
192 return new InsetCaption(buf);
194 case LFUN_INDEX_PRINT: {
195 InsetCommandParams icp(INDEX_PRINT_CODE);
196 icp["type"] = cmd.argument();
197 return new InsetPrintIndex(buf, icp);
200 case LFUN_NOMENCL_PRINT: {
201 InsetCommandParams icp(NOMENCL_PRINT_CODE);
202 icp["set_width"] = from_ascii("auto");
203 return new InsetPrintNomencl(buf, icp);
206 case LFUN_TOC_INSERT:
207 return new InsetTOC(buf, InsetCommandParams(TOC_CODE));
209 case LFUN_INFO_INSERT: {
210 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
215 case LFUN_PREVIEW_INSERT:
216 return new InsetPreview(buf);
218 case LFUN_INSET_INSERT: {
219 string const name = cmd.getArg(0);
220 InsetCode code = insetCode(name);
223 lyxerr << "No such inset '" << name << "'.";
227 InsetCommandParams icp(code);
228 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
229 return new InsetBibitem(buf, icp);
233 InsetCommandParams icp(code);
234 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
235 return new InsetBibtex(buf, icp);
239 InsetCommandParams icp(code);
240 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
241 return new InsetCitation(buf, icp);
245 return new InsetERT(buf,
246 InsetERT::string2params(to_utf8(cmd.argument())));
249 case LISTINGS_CODE: {
250 InsetListingsParams par;
251 InsetListings::string2params(to_utf8(cmd.argument()), par);
252 return new InsetListings(buf, par);
255 case EXTERNAL_CODE: {
256 InsetExternalParams iep;
257 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
258 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
259 inset->setBuffer(*buf);
260 inset->setParams(iep);
261 return inset.release();
264 case GRAPHICS_CODE: {
265 InsetGraphicsParams igp;
266 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
267 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
268 inset->setParams(igp);
269 return inset.release();
272 case HYPERLINK_CODE: {
273 InsetCommandParams icp(code);
274 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
275 return new InsetHyperlink(buf, icp);
279 InsetCommandParams icp(code);
280 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
281 return new InsetInclude(buf, icp);
285 docstring arg = cmd.argument();
286 return new InsetIndex(buf, InsetIndexParams(arg));
289 case INDEX_PRINT_CODE: {
290 InsetCommandParams icp(code);
291 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
292 return new InsetPrintIndex(buf, icp);
296 InsetCommandParams icp(code);
297 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
298 return new InsetNomencl(buf, icp);
302 InsetCommandParams icp(code);
303 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
304 return new InsetLabel(buf, icp);
308 InsetCommandParams icp(code);
309 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
310 return new InsetRef(buf, icp);
314 InsetSpaceParams isp;
315 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
316 return new InsetSpace(isp);
320 InsetCommandParams icp(code);
321 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
322 return new InsetTOC(buf, icp);
327 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
328 return new InsetVSpace(vspace);
332 return new InsetPreview(buf);
335 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
340 } //end LFUN_INSET_INSERT
342 case LFUN_SPACE_INSERT: {
343 string const name = cmd.getArg(0);
344 string const len = cmd.getArg(1);
346 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
349 InsetSpaceParams isp;
350 // The tests for isp.math might be disabled after a file format change
351 if (name == "normal")
352 isp.kind = InsetSpaceParams::NORMAL;
353 else if (name == "protected")
354 isp.kind = InsetSpaceParams::PROTECTED;
355 else if (name == "thin")
356 isp.kind = InsetSpaceParams::THIN;
357 else if (isp.math && name == "med")
358 isp.kind = InsetSpaceParams::MEDIUM;
359 else if (isp.math && name == "thick")
360 isp.kind = InsetSpaceParams::THICK;
361 else if (name == "quad")
362 isp.kind = InsetSpaceParams::QUAD;
363 else if (name == "qquad")
364 isp.kind = InsetSpaceParams::QQUAD;
365 else if (name == "enspace")
366 isp.kind = InsetSpaceParams::ENSPACE;
367 else if (name == "enskip")
368 isp.kind = InsetSpaceParams::ENSKIP;
369 else if (name == "negthinspace")
370 isp.kind = InsetSpaceParams::NEGTHIN;
371 else if (isp.math && name == "negmedspace")
372 isp.kind = InsetSpaceParams::NEGMEDIUM;
373 else if (isp.math && name == "negthickspace")
374 isp.kind = InsetSpaceParams::NEGTHICK;
375 else if (name == "hfill")
376 isp.kind = InsetSpaceParams::HFILL;
377 else if (name == "hfill*")
378 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
379 else if (name == "dotfill")
380 isp.kind = InsetSpaceParams::DOTFILL;
381 else if (name == "hrulefill")
382 isp.kind = InsetSpaceParams::HRULEFILL;
383 else if (name == "hspace") {
384 if (len.empty() || !isValidGlueLength(len)) {
385 lyxerr << "LyX function 'space-insert hspace' "
386 << "needs a valid length argument." << endl;
389 isp.kind = InsetSpaceParams::CUSTOM;
390 isp.length = GlueLength(len);
392 else if (name == "hspace*") {
393 if (len.empty() || !isValidGlueLength(len)) {
394 lyxerr << "LyX function 'space-insert hspace*' "
395 << "needs a valid length argument." << endl;
398 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
399 isp.length = GlueLength(len);
402 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
405 return new InsetSpace(isp);
413 } catch (ExceptionMessage const & message) {
414 if (message.type_ == ErrorException) {
415 // This should never happen!
416 Alert::error(message.title_, message.details_);
418 } else if (message.type_ == WarningException) {
419 Alert::warning(message.title_, message.details_);
428 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
430 Inset * inset = createInsetHelper(buf, cmd);
432 inset->setBuffer(*buf);
437 Inset * readInset(Lexer & lex, Buffer * buf)
440 if (lex.getString() != "\\begin_inset")
441 LYXERR0("Buffer::readInset: Consistency check failed.");
443 auto_ptr<Inset> inset;
448 // test the different insets
450 // FIXME It would be better if we did not have this branch and could
451 // just do one massive switch for all insets. But at present, it's
452 // easier to do it this way, and we can't do the massive switch until
453 // the conversion mentioned below. Note that if we do want to do a
454 // single switch, we need to remove this "CommandInset" line---or
455 // replace it with a single "InsetType" line that would be used in all
457 if (tmptok == "CommandInset") {
459 string const insetType = lex.getString();
460 lex.pushToken(insetType);
462 InsetCode const code = insetCode(insetType);
464 //FIXME If we do the one massive switch, we cannot do this here, since
465 //we do not know in advance that we're dealing with a command inset.
466 //Worst case, we could put it in each case below. Better, we could
467 //pass the lexer to the constructor and let the params be built there.
468 InsetCommandParams inscmd(code);
473 inset.reset(new InsetBibitem(buf, inscmd));
476 inset.reset(new InsetBibtex(buf, inscmd));
479 inset.reset(new InsetCitation(buf, inscmd));
482 inset.reset(new InsetHyperlink(buf, inscmd));
485 inset.reset(new InsetInclude(buf, inscmd));
487 case INDEX_PRINT_CODE:
488 inset.reset(new InsetPrintIndex(buf, inscmd));
491 inset.reset(new InsetLabel(buf, inscmd));
494 inset.reset(new InsetNomencl(buf, inscmd));
496 case NOMENCL_PRINT_CODE:
497 inset.reset(new InsetPrintNomencl(buf, inscmd));
500 if (inscmd["name"].empty() && inscmd["reference"].empty())
502 inset.reset(new InsetRef(buf, inscmd));
505 inset.reset(new InsetTOC(buf, inscmd));
509 lyxerr << "unknown CommandInset '" << insetType
511 while (lex.isOK() && lex.getString() != "\\end_inset")
515 inset->setBuffer(*buf);
517 // FIXME This branch should be made to use inset codes as the preceding
518 // branch does. Unfortunately, that will take some doing. It requires
519 // converting the representation of the insets in LyX files so that they
520 // use the inset names listed in Inset.cpp. Then, as above, the inset names
521 // can be translated to inset codes using insetCode(). And the insets'
522 // write() routines should use insetName() rather than hardcoding it.
523 if (tmptok == "Quotes") {
524 inset.reset(new InsetQuotes(buf));
525 } else if (tmptok == "External") {
526 inset.reset(new InsetExternal(buf));
527 } else if (tmptok == "FormulaMacro") {
528 inset.reset(new MathMacroTemplate(buf));
529 } else if (tmptok == "Formula") {
530 inset.reset(new InsetMathHull(buf));
531 } else if (tmptok == "Graphics") {
532 inset.reset(new InsetGraphics(buf));
533 } else if (tmptok == "Note") {
534 inset.reset(new InsetNote(buf, tmptok));
535 } else if (tmptok == "Box") {
536 inset.reset(new InsetBox(buf, tmptok));
537 } else if (tmptok == "Flex") {
539 string s = lex.getString();
540 inset.reset(new InsetFlex(buf, s));
541 } else if (tmptok == "Branch") {
542 inset.reset(new InsetBranch(buf, InsetBranchParams()));
543 } else if (tmptok == "Phantom") {
544 inset.reset(new InsetPhantom(buf, tmptok));
545 } else if (tmptok == "ERT") {
546 inset.reset(new InsetERT(buf));
547 } else if (tmptok == "listings") {
548 inset.reset(new InsetListings(buf));
549 } else if (tmptok == "space") {
550 inset.reset(new InsetSpace);
551 } else if (tmptok == "Tabular") {
552 inset.reset(new InsetTabular(buf));
553 } else if (tmptok == "Text") {
554 inset.reset(new InsetText(buf));
555 } else if (tmptok == "VSpace") {
556 inset.reset(new InsetVSpace);
557 } else if (tmptok == "Foot") {
558 inset.reset(new InsetFoot(buf));
559 } else if (tmptok == "Marginal") {
560 inset.reset(new InsetMarginal(buf));
561 } else if (tmptok == "Newpage") {
562 inset.reset(new InsetNewpage);
563 } else if (tmptok == "Newline") {
564 inset.reset(new InsetNewline);
565 } else if (tmptok == "Argument") {
566 inset.reset(new InsetArgument(buf));
567 } else if (tmptok == "Float") {
568 inset.reset(new InsetFloat(buf, string()));
569 } else if (tmptok == "Wrap") {
571 string tmptok = lex.getString();
572 inset.reset(new InsetWrap(buf, tmptok));
573 } else if (tmptok == "Caption") {
574 inset.reset(new InsetCaption(buf));
575 } else if (tmptok == "Index") {
576 inset.reset(new InsetIndex(buf, InsetIndexParams()));
577 } else if (tmptok == "FloatList") {
578 inset.reset(new InsetFloatList(buf));
579 } else if (tmptok == "Info") {
580 inset.reset(new InsetInfo(buf));
581 } else if (tmptok == "Preview") {
582 inset.reset(new InsetPreview(buf));
584 lyxerr << "unknown Inset type '" << tmptok
586 while (lex.isOK() && lex.getString() != "\\end_inset")
591 // Set the buffer reference for proper parsing of some insets
592 // (InsetCollapsable for example)
593 inset->setBuffer(*buf);
595 // Set again the buffer for insets that are created inside this inset
596 // (InsetMathHull for example).
597 inset->setBuffer(*buf);
599 return inset.release();