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_INFO_INSERT: {
204 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
209 case LFUN_PREVIEW_INSERT:
210 return new InsetPreview(buf);
212 case LFUN_INSET_INSERT: {
213 string const name = cmd.getArg(0);
214 InsetCode code = insetCode(name);
217 lyxerr << "No such inset '" << name << "'.";
221 InsetCommandParams icp(code);
222 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
223 return new InsetBibitem(buf, icp);
227 InsetCommandParams icp(code);
228 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
229 return new InsetBibtex(buf, icp);
233 InsetCommandParams icp(code);
234 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
235 return new InsetCitation(buf, icp);
239 return new InsetERT(buf,
240 InsetERT::string2params(to_utf8(cmd.argument())));
243 case EXTERNAL_CODE: {
244 InsetExternalParams iep;
245 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
246 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
247 inset->setBuffer(*buf);
248 inset->setParams(iep);
249 return inset.release();
252 case GRAPHICS_CODE: {
253 InsetGraphicsParams igp;
254 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
255 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
256 inset->setParams(igp);
257 return inset.release();
260 case HYPERLINK_CODE: {
261 InsetCommandParams icp(code);
262 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
263 return new InsetHyperlink(buf, icp);
267 InsetCommandParams icp(code);
268 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
269 return new InsetInclude(buf, icp);
273 docstring arg = cmd.argument();
274 return new InsetIndex(buf, InsetIndexParams(arg));
277 case INDEX_PRINT_CODE: {
278 InsetCommandParams icp(code);
279 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
280 return new InsetPrintIndex(buf, icp);
284 InsetCommandParams icp(code);
285 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
286 return new InsetLabel(buf, icp);
290 InsetCommandParams icp(code);
291 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
292 return new InsetLine(buf, icp);
295 case LISTINGS_CODE: {
296 InsetListingsParams par;
297 InsetListings::string2params(to_utf8(cmd.argument()), par);
298 return new InsetListings(buf, par);
302 InsetCommandParams icp(code);
303 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
304 return new InsetNomencl(buf, icp);
308 InsetCommandParams icp(code);
309 InsetCommand::string2params(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(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 InsetLine(buf, inscmd));
497 inset.reset(new InsetNomencl(buf, inscmd));
499 case NOMENCL_PRINT_CODE:
500 inset.reset(new InsetPrintNomencl(buf, inscmd));
503 if (inscmd["name"].empty() && inscmd["reference"].empty())
505 inset.reset(new InsetRef(buf, inscmd));
508 inset.reset(new InsetTOC(buf, inscmd));
512 lyxerr << "unknown CommandInset '" << insetType
514 while (lex.isOK() && lex.getString() != "\\end_inset")
518 inset->setBuffer(*buf);
520 // FIXME This branch should be made to use inset codes as the preceding
521 // branch does. Unfortunately, that will take some doing. It requires
522 // converting the representation of the insets in LyX files so that they
523 // use the inset names listed in Inset.cpp. Then, as above, the inset names
524 // can be translated to inset codes using insetCode(). And the insets'
525 // write() routines should use insetName() rather than hardcoding it.
526 if (tmptok == "Quotes") {
527 inset.reset(new InsetQuotes(buf));
528 } else if (tmptok == "External") {
529 inset.reset(new InsetExternal(buf));
530 } else if (tmptok == "FormulaMacro") {
531 inset.reset(new MathMacroTemplate(buf));
532 } else if (tmptok == "Formula") {
533 inset.reset(new InsetMathHull(buf));
534 } else if (tmptok == "Graphics") {
535 inset.reset(new InsetGraphics(buf));
536 } else if (tmptok == "Note") {
537 inset.reset(new InsetNote(buf, tmptok));
538 } else if (tmptok == "Box") {
539 inset.reset(new InsetBox(buf, tmptok));
540 } else if (tmptok == "Flex") {
542 string s = lex.getString();
543 inset.reset(new InsetFlex(buf, s));
544 } else if (tmptok == "Branch") {
545 inset.reset(new InsetBranch(buf, InsetBranchParams()));
546 } else if (tmptok == "Phantom") {
547 inset.reset(new InsetPhantom(buf, tmptok));
548 } else if (tmptok == "ERT") {
549 inset.reset(new InsetERT(buf));
550 } else if (tmptok == "listings") {
551 inset.reset(new InsetListings(buf));
552 } else if (tmptok == "space") {
553 inset.reset(new InsetSpace);
554 } else if (tmptok == "Tabular") {
555 inset.reset(new InsetTabular(buf));
556 } else if (tmptok == "Text") {
557 inset.reset(new InsetText(buf));
558 } else if (tmptok == "VSpace") {
559 inset.reset(new InsetVSpace);
560 } else if (tmptok == "Foot") {
561 inset.reset(new InsetFoot(buf));
562 } else if (tmptok == "Marginal") {
563 inset.reset(new InsetMarginal(buf));
564 } else if (tmptok == "Newpage") {
565 inset.reset(new InsetNewpage);
566 } else if (tmptok == "Newline") {
567 inset.reset(new InsetNewline);
568 } else if (tmptok == "Argument") {
569 inset.reset(new InsetArgument(buf));
570 } else if (tmptok == "Float") {
571 inset.reset(new InsetFloat(buf, string()));
572 } else if (tmptok == "Wrap") {
574 string tmptok = lex.getString();
575 inset.reset(new InsetWrap(buf, tmptok));
576 } else if (tmptok == "Caption") {
577 inset.reset(new InsetCaption(buf));
578 } else if (tmptok == "Index") {
579 inset.reset(new InsetIndex(buf, InsetIndexParams()));
580 } else if (tmptok == "FloatList") {
581 inset.reset(new InsetFloatList(buf));
582 } else if (tmptok == "Info") {
583 inset.reset(new InsetInfo(buf));
584 } else if (tmptok == "Preview") {
585 inset.reset(new InsetPreview(buf));
587 lyxerr << "unknown Inset type '" << tmptok
589 while (lex.isOK() && lex.getString() != "\\end_inset")
594 // Set the buffer reference for proper parsing of some insets
595 // (InsetCollapsable for example)
596 inset->setBuffer(*buf);
598 // Set again the buffer for insets that are created inside this inset
599 // (InsetMathHull for example).
600 inset->setBuffer(*buf);
602 return inset.release();