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/InsetOptArg.h"
49 #include "insets/InsetPhantom.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.h"
54 #include "insets/InsetVSpace.h"
55 #include "insets/InsetWrap.h"
57 #include "mathed/MathMacroTemplate.h"
58 #include "mathed/InsetMathHull.h"
60 #include "frontends/alert.h"
62 #include "support/debug.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
66 #include "support/lassert.h"
71 using namespace lyx::support;
75 namespace Alert = frontend::Alert;
78 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
84 case LFUN_LINE_INSERT:
87 case LFUN_NEWPAGE_INSERT: {
88 string const name = cmd.getArg(0);
89 InsetNewpageParams inp;
90 if (name.empty() || name == "newpage")
91 inp.kind = InsetNewpageParams::NEWPAGE;
92 else if (name == "pagebreak")
93 inp.kind = InsetNewpageParams::PAGEBREAK;
94 else if (name == "clearpage")
95 inp.kind = InsetNewpageParams::CLEARPAGE;
96 else if (name == "cleardoublepage")
97 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
98 return new InsetNewpage(inp);
101 case LFUN_FLEX_INSERT: {
102 string s = cmd.getArg(0);
103 return new InsetFlex(buf, s);
106 case LFUN_NOTE_INSERT: {
107 string arg = cmd.getArg(0);
110 return new InsetNote(buf, arg);
113 case LFUN_BOX_INSERT: {
114 string arg = cmd.getArg(0);
117 return new InsetBox(buf, arg);
120 case LFUN_BRANCH_INSERT: {
121 docstring arg = cmd.argument();
123 arg = from_ascii("none");
124 return new InsetBranch(buf, InsetBranchParams(arg));
127 case LFUN_PHANTOM_INSERT: {
128 string arg = cmd.getArg(0);
131 return new InsetPhantom(buf, arg);
134 case LFUN_ERT_INSERT:
135 return new InsetERT(buf);
137 case LFUN_LISTING_INSERT:
138 return new InsetListings(buf);
140 case LFUN_FOOTNOTE_INSERT:
141 return new InsetFoot(buf);
143 case LFUN_MARGINALNOTE_INSERT:
144 return new InsetMarginal(buf);
146 case LFUN_OPTIONAL_INSERT:
147 return new InsetOptArg(buf);
149 case LFUN_FLOAT_INSERT:
150 return new InsetFloat(buf, to_utf8(cmd.argument()));
152 case LFUN_FLOAT_WIDE_INSERT: {
153 InsetFloat * fl = new InsetFloat(buf, to_utf8(cmd.argument()));
158 case LFUN_WRAP_INSERT: {
159 string const argument = to_utf8(cmd.argument());
160 if (argument == "figure" || argument == "table")
161 return new InsetWrap(buf, argument);
162 lyxerr << "Non-existent wrapfig type: " << argument << endl;
166 case LFUN_INDEX_INSERT: {
167 docstring arg = cmd.argument();
168 return new InsetIndex(buf, InsetIndexParams(arg));
171 case LFUN_NOMENCL_INSERT: {
172 InsetCommandParams icp(NOMENCL_CODE);
173 icp["symbol"] = cmd.argument();
174 return new InsetNomencl(buf, icp);
177 case LFUN_TABULAR_INSERT: {
178 if (cmd.argument().empty())
180 istringstream ss(to_utf8(cmd.argument()));
187 return new InsetTabular(buf, r, c);
190 case LFUN_CAPTION_INSERT:
191 return new InsetCaption(buf);
193 case LFUN_INDEX_PRINT: {
194 InsetCommandParams icp(INDEX_PRINT_CODE);
195 icp["type"] = cmd.argument();
196 return new InsetPrintIndex(buf, icp);
199 case LFUN_NOMENCL_PRINT: {
200 InsetCommandParams icp(NOMENCL_PRINT_CODE);
201 icp["set_width"] = from_ascii("auto");
202 return new InsetPrintNomencl(buf, icp);
205 case LFUN_TOC_INSERT:
206 return new InsetTOC(buf, InsetCommandParams(TOC_CODE));
208 case LFUN_INFO_INSERT: {
209 InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
214 case LFUN_INSET_INSERT: {
215 string const name = cmd.getArg(0);
216 InsetCode code = insetCode(name);
219 lyxerr << "No such inset '" << name << "'.";
223 InsetCommandParams icp(code);
224 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
225 return new InsetBibitem(buf, icp);
229 InsetCommandParams icp(code);
230 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
231 return new InsetBibtex(buf, icp);
235 InsetCommandParams icp(code);
236 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
237 return new InsetCitation(buf, icp);
241 return new InsetERT(buf,
242 InsetERT::string2params(to_utf8(cmd.argument())));
245 case LISTINGS_CODE: {
246 InsetListingsParams par;
247 InsetListings::string2params(to_utf8(cmd.argument()), par);
248 return new InsetListings(buf, par);
251 case EXTERNAL_CODE: {
252 InsetExternalParams iep;
253 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
254 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
255 inset->setBuffer(*buf);
256 inset->setParams(iep);
257 return inset.release();
260 case GRAPHICS_CODE: {
261 InsetGraphicsParams igp;
262 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
263 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
264 inset->setParams(igp);
265 return inset.release();
268 case HYPERLINK_CODE: {
269 InsetCommandParams icp(code);
270 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
271 return new InsetHyperlink(buf, icp);
275 InsetCommandParams icp(code);
276 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
277 return new InsetInclude(buf, icp);
281 docstring arg = cmd.argument();
282 return new InsetIndex(buf, InsetIndexParams(arg));
285 case INDEX_PRINT_CODE: {
286 InsetCommandParams icp(code);
287 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
288 return new InsetPrintIndex(buf, icp);
292 InsetCommandParams icp(code);
293 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
294 return new InsetNomencl(buf, icp);
298 InsetCommandParams icp(code);
299 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
300 return new InsetLabel(buf, icp);
304 InsetCommandParams icp(code);
305 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
306 return new InsetRef(buf, icp);
310 InsetSpaceParams isp;
311 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
312 return new InsetSpace(isp);
316 InsetCommandParams icp(code);
317 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
318 return new InsetTOC(buf, icp);
323 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
324 return new InsetVSpace(vspace);
328 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
333 } //end LFUN_INSET_INSERT
335 case LFUN_SPACE_INSERT: {
336 string const name = cmd.getArg(0);
337 string const len = cmd.getArg(1);
339 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
342 InsetSpaceParams isp;
343 // The tests for isp.math might be disabled after a file format change
344 if (name == "normal")
345 isp.kind = InsetSpaceParams::NORMAL;
346 else if (name == "protected")
347 isp.kind = InsetSpaceParams::PROTECTED;
348 else if (name == "thin")
349 isp.kind = InsetSpaceParams::THIN;
350 else if (isp.math && name == "med")
351 isp.kind = InsetSpaceParams::MEDIUM;
352 else if (isp.math && name == "thick")
353 isp.kind = InsetSpaceParams::THICK;
354 else if (name == "quad")
355 isp.kind = InsetSpaceParams::QUAD;
356 else if (name == "qquad")
357 isp.kind = InsetSpaceParams::QQUAD;
358 else if (name == "enspace")
359 isp.kind = InsetSpaceParams::ENSPACE;
360 else if (name == "enskip")
361 isp.kind = InsetSpaceParams::ENSKIP;
362 else if (name == "negthinspace")
363 isp.kind = InsetSpaceParams::NEGTHIN;
364 else if (isp.math && name == "negmedspace")
365 isp.kind = InsetSpaceParams::NEGMEDIUM;
366 else if (isp.math && name == "negthickspace")
367 isp.kind = InsetSpaceParams::NEGTHICK;
368 else if (name == "hfill")
369 isp.kind = InsetSpaceParams::HFILL;
370 else if (name == "hfill*")
371 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
372 else if (name == "dotfill")
373 isp.kind = InsetSpaceParams::DOTFILL;
374 else if (name == "hrulefill")
375 isp.kind = InsetSpaceParams::HRULEFILL;
376 else if (name == "hspace") {
377 if (len.empty() || !isValidGlueLength(len)) {
378 lyxerr << "LyX function 'space-insert hspace' "
379 << "needs a valid length argument." << endl;
382 isp.kind = InsetSpaceParams::CUSTOM;
383 isp.length = GlueLength(len);
385 else if (name == "hspace*") {
386 if (len.empty() || !isValidGlueLength(len)) {
387 lyxerr << "LyX function 'space-insert hspace*' "
388 << "needs a valid length argument." << endl;
391 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
392 isp.length = GlueLength(len);
395 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
398 return new InsetSpace(isp);
406 } catch (ExceptionMessage const & message) {
407 if (message.type_ == ErrorException) {
408 // This should never happen!
409 Alert::error(message.title_, message.details_);
411 } else if (message.type_ == WarningException) {
412 Alert::warning(message.title_, message.details_);
421 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
423 Inset * inset = createInsetHelper(buf, cmd);
425 inset->setBuffer(*buf);
430 Inset * readInset(Lexer & lex, Buffer * buf)
433 if (lex.getString() != "\\begin_inset")
434 LYXERR0("Buffer::readInset: Consistency check failed.");
436 auto_ptr<Inset> inset;
441 // test the different insets
443 // FIXME It would be better if we did not have this branch and could
444 // just do one massive switch for all insets. But at present, it's
445 // easier to do it this way, and we can't do the massive switch until
446 // the conversion mentioned below. Note that if we do want to do a
447 // single switch, we need to remove this "CommandInset" line---or
448 // replace it with a single "InsetType" line that would be used in all
450 if (tmptok == "CommandInset") {
452 string const insetType = lex.getString();
453 lex.pushToken(insetType);
455 InsetCode const code = insetCode(insetType);
457 //FIXME If we do the one massive switch, we cannot do this here, since
458 //we do not know in advance that we're dealing with a command inset.
459 //Worst case, we could put it in each case below. Better, we could
460 //pass the lexer to the constructor and let the params be built there.
461 InsetCommandParams inscmd(code);
466 inset.reset(new InsetBibitem(buf, inscmd));
469 inset.reset(new InsetBibtex(buf, inscmd));
472 inset.reset(new InsetCitation(buf, inscmd));
475 inset.reset(new InsetHyperlink(buf, inscmd));
478 inset.reset(new InsetInclude(buf, inscmd));
480 case INDEX_PRINT_CODE:
481 inset.reset(new InsetPrintIndex(buf, inscmd));
484 inset.reset(new InsetLabel(buf, inscmd));
487 inset.reset(new InsetNomencl(buf, inscmd));
489 case NOMENCL_PRINT_CODE:
490 inset.reset(new InsetPrintNomencl(buf, inscmd));
493 if (inscmd["name"].empty() && inscmd["reference"].empty())
495 inset.reset(new InsetRef(buf, inscmd));
498 inset.reset(new InsetTOC(buf, inscmd));
502 lyxerr << "unknown CommandInset '" << insetType
504 while (lex.isOK() && lex.getString() != "\\end_inset")
508 inset->setBuffer(*buf);
510 // FIXME This branch should be made to use inset codes as the preceding
511 // branch does. Unfortunately, that will take some doing. It requires
512 // converting the representation of the insets in LyX files so that they
513 // use the inset names listed in Inset.cpp. Then, as above, the inset names
514 // can be translated to inset codes using insetCode(). And the insets'
515 // write() routines should use insetName() rather than hardcoding it.
516 if (tmptok == "Quotes") {
517 inset.reset(new InsetQuotes(buf));
518 } else if (tmptok == "External") {
519 inset.reset(new InsetExternal(buf));
520 } else if (tmptok == "FormulaMacro") {
521 inset.reset(new MathMacroTemplate(buf));
522 } else if (tmptok == "Formula") {
523 inset.reset(new InsetMathHull(buf));
524 } else if (tmptok == "Graphics") {
525 inset.reset(new InsetGraphics(buf));
526 } else if (tmptok == "Note") {
527 inset.reset(new InsetNote(buf, tmptok));
528 } else if (tmptok == "Box") {
529 inset.reset(new InsetBox(buf, tmptok));
530 } else if (tmptok == "Flex") {
532 string s = lex.getString();
533 inset.reset(new InsetFlex(buf, s));
534 } else if (tmptok == "Branch") {
535 inset.reset(new InsetBranch(buf, InsetBranchParams()));
536 } else if (tmptok == "Phantom") {
537 inset.reset(new InsetPhantom(buf, tmptok));
538 } else if (tmptok == "ERT") {
539 inset.reset(new InsetERT(buf));
540 } else if (tmptok == "listings") {
541 inset.reset(new InsetListings(buf));
542 } else if (tmptok == "space") {
543 inset.reset(new InsetSpace);
544 } else if (tmptok == "Tabular") {
545 inset.reset(new InsetTabular(buf));
546 } else if (tmptok == "Text") {
547 inset.reset(new InsetText(buf));
548 } else if (tmptok == "VSpace") {
549 inset.reset(new InsetVSpace);
550 } else if (tmptok == "Foot") {
551 inset.reset(new InsetFoot(buf));
552 } else if (tmptok == "Marginal") {
553 inset.reset(new InsetMarginal(buf));
554 } else if (tmptok == "Newpage") {
555 inset.reset(new InsetNewpage);
556 } else if (tmptok == "Newline") {
557 inset.reset(new InsetNewline);
558 } else if (tmptok == "OptArg") {
559 inset.reset(new InsetOptArg(buf));
560 } else if (tmptok == "Float") {
561 inset.reset(new InsetFloat(buf, string()));
562 } else if (tmptok == "Wrap") {
564 string tmptok = lex.getString();
565 inset.reset(new InsetWrap(buf, tmptok));
566 } else if (tmptok == "Caption") {
567 inset.reset(new InsetCaption(buf));
568 } else if (tmptok == "Index") {
569 inset.reset(new InsetIndex(buf, InsetIndexParams()));
570 } else if (tmptok == "FloatList") {
571 inset.reset(new InsetFloatList(buf));
572 } else if (tmptok == "Info") {
573 inset.reset(new InsetInfo(buf));
575 lyxerr << "unknown Inset type '" << tmptok
577 while (lex.isOK() && lex.getString() != "\\end_inset")
582 // Set the buffer reference for proper parsing of some insets
583 // (InsetCollapsable for example)
584 inset->setBuffer(*buf);
586 // Set again the buffer for insets that are created inside this inset
587 // (InsetMathHull for example).
588 inset->setBuffer(*buf);
590 return inset.release();