]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
Update RELEASE-NOTES
[lyx.git] / src / factory.cpp
1 /**
2  * \file factory.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "factory.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "FloatList.h"
18 #include "FuncRequest.h"
19 #include "Lexer.h"
20 #include "LyX.h"
21 #include "TextClass.h"
22
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/InsetIPA.h"
42 #include "insets/InsetIPAMacro.h"
43 #include "insets/InsetLabel.h"
44 #include "insets/InsetLine.h"
45 #include "insets/InsetMarginal.h"
46 #include "insets/InsetNewline.h"
47 #include "insets/InsetNewpage.h"
48 #include "insets/InsetNomencl.h"
49 #include "insets/InsetNote.h"
50 #include "insets/InsetArgument.h"
51 #include "insets/InsetPhantom.h"
52 #include "insets/InsetPreview.h"
53 #include "insets/InsetRef.h"
54 #include "insets/InsetScript.h"
55 #include "insets/InsetSpace.h"
56 #include "insets/InsetTabular.h"
57 #include "insets/InsetTOC.h"
58 #include "insets/InsetVSpace.h"
59 #include "insets/InsetWrap.h"
60
61 #include "mathed/MathMacroTemplate.h"
62 #include "mathed/InsetMathHull.h"
63
64 #include "frontends/alert.h"
65
66 #include "support/debug.h"
67 #include "support/lstrings.h"
68 #include "support/ExceptionMessage.h"
69
70 #include "support/lassert.h"
71
72 #include <sstream>
73
74 using namespace std;
75 using namespace lyx::support;
76
77 namespace lyx {
78
79 namespace Alert = frontend::Alert;
80
81
82 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
83 {
84         try {
85
86                 switch (cmd.action()) {
87
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);
100                 }
101
102                 case LFUN_FLEX_INSERT: {
103                         string s = cmd.getArg(0);
104                         return new InsetFlex(buf, s);
105                 }
106
107                 case LFUN_NOTE_INSERT: {
108                         string arg = cmd.getArg(0);
109                         if (arg.empty())
110                                 arg = "Note";
111                         return new InsetNote(buf, arg);
112                 }
113
114                 case LFUN_BOX_INSERT: {
115                         string arg = cmd.getArg(0);
116                         if (arg.empty())
117                                 arg = "Boxed";
118                         return new InsetBox(buf, arg);
119                 }
120
121                 case LFUN_BRANCH_INSERT: {
122                         docstring arg = cmd.argument();
123                         if (arg.empty())
124                                 arg = from_ascii("none");
125                         return new InsetBranch(buf, InsetBranchParams(arg));
126                 }
127
128                 case LFUN_PHANTOM_INSERT: {
129                         string arg = cmd.getArg(0);
130                         if (arg.empty())
131                                 arg = "Phantom";
132                         return new InsetPhantom(buf, arg);
133                 }
134
135                 case LFUN_IPAMACRO_INSERT: {
136                         string const arg1 = cmd.getArg(0);
137                         string const arg2 = cmd.getArg(1);
138                         if (arg1 != "deco") {
139                                 LYXERR0("LFUN_IPAMACRO_INSERT: wrong argument");
140                                 return 0;
141                         }
142                         return new InsetIPADeco(buf, arg2);
143                 }
144
145                 case LFUN_ERT_INSERT:
146                         return new InsetERT(buf);
147
148                 case LFUN_LISTING_INSERT:
149                         return new InsetListings(buf);
150
151                 case LFUN_FOOTNOTE_INSERT:
152                         return new InsetFoot(buf);
153
154                 case LFUN_MARGINALNOTE_INSERT:
155                         return new InsetMarginal(buf);
156
157                 case LFUN_ARGUMENT_INSERT: {
158                         string arg = cmd.getArg(0);
159                         if (arg.empty()) {
160                                 LYXERR0("argument-insert needs an argument!");
161                                 return 0;
162                         }
163                         return new InsetArgument(buf, arg);
164                 }
165
166                 case LFUN_FLOAT_INSERT: {
167                         string argument = to_utf8(cmd.argument());
168                         if (!argument.empty()) {
169                                 if (!contains(argument, "sideways")) {
170                                         if (!contains(argument, "wide"))
171                                                 argument += "\nwide false";
172                                         argument += "\nsideways false";
173                                 }
174                         }
175                         return new InsetFloat(buf, argument);
176                 }
177
178                 case LFUN_FLOAT_WIDE_INSERT: {
179                         string argument = to_utf8(cmd.argument());
180                         if (!argument.empty()) {
181                                 if (!contains(argument, "sideways")) {
182                                         if (!contains(argument, "wide"))
183                                                 argument += "\nwide true";
184                                         argument += "\nsideways false";
185                                 }
186                         }
187                         InsetFloat * fl = new InsetFloat(buf, argument);
188                         fl->setWide(true);
189                         return fl;
190                 }
191
192                 case LFUN_WRAP_INSERT: {
193                         string const argument = to_utf8(cmd.argument());
194                         if (argument == "figure" || argument == "table")
195                                 return new InsetWrap(buf, argument);
196                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
197                         return 0;
198                 }
199
200                 case LFUN_INDEX_INSERT: {
201                         docstring arg = cmd.argument();
202                         return new InsetIndex(buf, InsetIndexParams(arg));
203                 }
204
205                 case LFUN_IPA_INSERT:
206                         return new InsetIPA(buf);
207
208                 case LFUN_NOMENCL_INSERT: {
209                         InsetCommandParams icp(NOMENCL_CODE);
210                         icp["symbol"] = cmd.argument();
211                         return new InsetNomencl(buf, icp);
212                 }
213
214                 case LFUN_TABULAR_INSERT: {
215                         if (cmd.argument().empty())
216                                 return 0;
217                         istringstream ss(to_utf8(cmd.argument()));
218                         int r = 0, c = 0;
219                         ss >> r >> c;
220                         if (r <= 0)
221                                 r = 2;
222                         if (c <= 0)
223                                 c = 2;
224                         return new InsetTabular(buf, r, c);
225                 }
226
227                 case LFUN_CAPTION_INSERT: {
228                         string arg = cmd.getArg(0);
229                         if (arg.empty())
230                                 arg = "Standard";
231                         return new InsetCaption(buf, arg);
232                 }
233
234                 case LFUN_INDEX_PRINT:  {
235                         InsetCommandParams icp(INDEX_PRINT_CODE);
236                         icp["type"] = cmd.argument();
237                         return new InsetPrintIndex(buf, icp);
238                 }
239
240                 case LFUN_NOMENCL_PRINT: {
241                         InsetCommandParams icp(NOMENCL_PRINT_CODE);
242                         icp["set_width"] = from_ascii("auto");
243                         return new InsetPrintNomencl(buf, icp);
244                 }
245
246                 case LFUN_INFO_INSERT: {
247                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
248                         inset->updateInfo();
249                         return inset;
250                 }
251
252                 case LFUN_PREVIEW_INSERT:
253                         return new InsetPreview(buf);
254
255                 case LFUN_SCRIPT_INSERT: {
256                         InsetScriptParams isp;
257                         InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
258                         return new InsetScript(buf, isp);
259                 }
260
261                 case LFUN_INSET_INSERT: {
262                         string const name = cmd.getArg(0);
263                         InsetCode code = insetCode(name);
264                         switch (code) {
265                         case NO_CODE:
266                                 lyxerr << "No such inset '" << name << "'.";
267                                 return 0;
268                         
269                         case BIBITEM_CODE: {
270                                 InsetCommandParams icp(code);
271                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
272                                 return new InsetBibitem(buf, icp);
273                         }
274                         
275                         case BIBTEX_CODE: {
276                                 InsetCommandParams icp(code);
277                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
278                                 return new InsetBibtex(buf, icp);
279                         }
280                         
281                         case CITE_CODE: {
282                                 InsetCommandParams icp(code);
283                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
284                                 return new InsetCitation(buf, icp);
285                         }
286                         
287                         case ERT_CODE: {
288                                 return new InsetERT(buf,
289                                         InsetERT::string2params(to_utf8(cmd.argument())));
290                         }
291                         
292                         case EXTERNAL_CODE: {
293                                 InsetExternalParams iep;
294                                 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
295                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
296                                 inset->setBuffer(*buf);
297                                 inset->setParams(iep);
298                                 return inset.release();
299                         }
300                         
301                         case GRAPHICS_CODE: {
302                                 InsetGraphicsParams igp;
303                                 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
304                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
305                                 inset->setParams(igp);
306                                 return inset.release();
307                         }
308                         
309                         case HYPERLINK_CODE: {
310                                 InsetCommandParams icp(code);
311                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
312                                 return new InsetHyperlink(buf, icp);
313                         }
314                         
315                         case INCLUDE_CODE: {
316                                 InsetCommandParams icp(code);
317                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
318                                 return new InsetInclude(buf, icp);
319                         }
320                         
321                         case INDEX_CODE: {
322                                 docstring arg = cmd.argument();
323                                 return new InsetIndex(buf, InsetIndexParams(arg));
324                         }
325                         
326                         case INDEX_PRINT_CODE:  {
327                                 InsetCommandParams icp(code);
328                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
329                                 return new InsetPrintIndex(buf, icp);
330                         }
331                         
332                         case LABEL_CODE: {
333                                 InsetCommandParams icp(code);
334                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
335                                 return new InsetLabel(buf, icp);
336                         }
337                         
338                         case LINE_CODE: {
339                                 InsetCommandParams icp(code);
340                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
341                                 return new InsetLine(buf, icp);
342                         }
343                                 
344                         case LISTINGS_CODE: {
345                                 InsetListingsParams par;
346                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
347                                 return new InsetListings(buf, par);
348                         }
349                         
350                         case NOMENCL_CODE: {
351                                 InsetCommandParams icp(code);
352                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
353                                 return new InsetNomencl(buf, icp);
354                         }
355                         
356                         case REF_CODE: {
357                                 InsetCommandParams icp(code);
358                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
359                                 return new InsetRef(buf, icp);
360                         }
361
362                         case SCRIPT_CODE: {
363                                 InsetScriptParams isp;
364                                 InsetScript::string2params(to_utf8(cmd.argument()), isp);
365                                 return new InsetScript(buf, isp);
366                         }
367
368                         case SPACE_CODE: {
369                                 InsetSpaceParams isp;
370                                 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
371                                 return new InsetSpace(isp);
372                         }
373                         
374                         case TOC_CODE: {
375                                 InsetCommandParams icp(code);
376                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
377                                 return new InsetTOC(buf, icp);
378                         }
379                         
380                         case VSPACE_CODE: {
381                                 VSpace vspace;
382                                 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
383                                 return new InsetVSpace(vspace);
384                         }
385
386                         case PREVIEW_CODE:
387                                 return new InsetPreview(buf);
388                         
389                         default:
390                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
391                                                 << endl;
392                                 return 0;
393                         
394                         }
395                 } //end LFUN_INSET_INSERT
396
397                 case LFUN_SPACE_INSERT: {
398                         string const name = cmd.getArg(0);
399                         string const len = cmd.getArg(1);
400                         if (name.empty()) {
401                                 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
402                                 break;
403                         }
404                         InsetSpaceParams isp;
405                         // The tests for isp.math might be disabled after a file format change
406                         if (name == "normal")
407                                 isp.kind = InsetSpaceParams::NORMAL;
408                         else if (name == "protected")
409                                 isp.kind = InsetSpaceParams::PROTECTED;
410                         else if (name == "visible")
411                                 isp.kind = InsetSpaceParams::VISIBLE;
412                         else if (name == "thin")
413                                 isp.kind = InsetSpaceParams::THIN;
414                         else if (isp.math && name == "med")
415                                 isp.kind = InsetSpaceParams::MEDIUM;
416                         else if (isp.math && name == "thick")
417                                 isp.kind = InsetSpaceParams::THICK;
418                         else if (name == "quad")
419                                 isp.kind = InsetSpaceParams::QUAD;
420                         else if (name == "qquad")
421                                 isp.kind = InsetSpaceParams::QQUAD;
422                         else if (name == "enspace")
423                                 isp.kind = InsetSpaceParams::ENSPACE;
424                         else if (name == "enskip")
425                                 isp.kind = InsetSpaceParams::ENSKIP;
426                         else if (name == "negthinspace")
427                                 isp.kind = InsetSpaceParams::NEGTHIN;
428                         else if (name == "negmedspace")
429                                 isp.kind = InsetSpaceParams::NEGMEDIUM;
430                         else if (name == "negthickspace")
431                                 isp.kind = InsetSpaceParams::NEGTHICK;
432                         else if (name == "hfill")
433                                 isp.kind = InsetSpaceParams::HFILL;
434                         else if (name == "hfill*")
435                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
436                         else if (name == "dotfill")
437                                 isp.kind = InsetSpaceParams::DOTFILL;
438                         else if (name == "hrulefill")
439                                 isp.kind = InsetSpaceParams::HRULEFILL;
440                         else if (name == "hspace") {
441                                 if (len.empty() || !isValidGlueLength(len)) {
442                                         lyxerr << "LyX function 'space-insert hspace' "
443                                                << "needs a valid length argument." << endl;
444                                         break;
445                                 }
446                                 isp.kind = InsetSpaceParams::CUSTOM;
447                                 isp.length = GlueLength(len);
448                         }
449                         else if (name == "hspace*") {
450                                 if (len.empty() || !isValidGlueLength(len)) {
451                                         lyxerr << "LyX function 'space-insert hspace*' "
452                                                << "needs a valid length argument." << endl;
453                                         break;
454                                 }
455                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
456                                 isp.length = GlueLength(len);
457                         }
458                         else {
459                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
460                                 break;
461                         }
462                         return new InsetSpace(isp);
463                 }
464                 break;
465
466                 default:
467                         break;
468                 }
469
470         } catch (ExceptionMessage const & message) {
471                 if (message.type_ == ErrorException) {
472                         // This should never happen!
473                         Alert::error(message.title_, message.details_);
474                         lyx_exit(1);
475                 } else if (message.type_ == WarningException) {
476                         Alert::warning(message.title_, message.details_);
477                         return 0;
478                 }
479         }
480
481         return 0;
482 }
483
484
485 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
486 {
487         Inset * inset = createInsetHelper(buf, cmd);
488         if (inset)
489                 inset->setBuffer(*buf);
490         return inset;
491 }
492
493
494 Inset * readInset(Lexer & lex, Buffer * buf)
495 {
496         // consistency check
497         if (lex.getString() != "\\begin_inset")
498                 LYXERR0("Buffer::readInset: Consistency check failed.");
499
500         auto_ptr<Inset> inset;
501
502         string tmptok;
503         lex >> tmptok;
504
505         // test the different insets
506         
507         // FIXME It would be better if we did not have this branch and could
508         // just do one massive switch for all insets. But at present, it's
509         // easier to do it this way, and we can't do the massive switch until
510         // the conversion mentioned below.  Note that if we do want to do a
511         // single switch, we need to remove this "CommandInset" line---or
512         // replace it with a single "InsetType" line that would be used in all
513         // insets.
514         if (tmptok == "CommandInset") {
515                 lex.next();
516                 string const insetType = lex.getString();
517                 lex.pushToken(insetType);
518                 
519                 InsetCode const code = insetCode(insetType);
520                 
521                 //FIXME If we do the one massive switch, we cannot do this here, since
522                 //we do not know in advance that we're dealing with a command inset.
523                 //Worst case, we could put it in each case below. Better, we could
524                 //pass the lexer to the constructor and let the params be built there.
525                 InsetCommandParams inscmd(code);
526                 inscmd.read(lex);
527
528                 switch (code) {
529                         case BIBITEM_CODE:
530                                 inset.reset(new InsetBibitem(buf, inscmd));
531                                 break;
532                         case BIBTEX_CODE:
533                                 inset.reset(new InsetBibtex(buf, inscmd));
534                                 break;
535                         case CITE_CODE: 
536                                 inset.reset(new InsetCitation(buf, inscmd));
537                                 break;
538                         case HYPERLINK_CODE:
539                                 inset.reset(new InsetHyperlink(buf, inscmd));
540                                 break;
541                         case INCLUDE_CODE:
542                                 inset.reset(new InsetInclude(buf, inscmd));
543                                 break;
544                         case INDEX_PRINT_CODE:
545                                 inset.reset(new InsetPrintIndex(buf, inscmd));
546                                 break;
547                         case LABEL_CODE:
548                                 inset.reset(new InsetLabel(buf, inscmd));
549                                 break;
550                         case LINE_CODE:
551                                 inset.reset(new InsetLine(buf, inscmd));
552                                 break;
553                         case NOMENCL_CODE:
554                                 inset.reset(new InsetNomencl(buf, inscmd));
555                                 break;
556                         case NOMENCL_PRINT_CODE:
557                                 inset.reset(new InsetPrintNomencl(buf, inscmd));
558                                 break;
559                         case REF_CODE:
560                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
561                                         return 0;
562                                 inset.reset(new InsetRef(buf, inscmd));
563                                 break;
564                         case TOC_CODE:
565                                 inset.reset(new InsetTOC(buf, inscmd));
566                                 break;
567                         case NO_CODE:
568                         default:
569                                 lyxerr << "unknown CommandInset '" << insetType
570                                                         << "'" << endl;
571                                 while (lex.isOK() && lex.getString() != "\\end_inset")
572                                         lex.next();
573                                 return 0;
574                 }
575                 inset->setBuffer(*buf);
576         } else { 
577                 // FIXME This branch should be made to use inset codes
578                 // as the preceding branch does. Unfortunately, that
579                 // will take some doing. It requires converting the
580                 // representation of the insets in LyX files so that
581                 // they use the inset names listed in Inset.cpp. Then,
582                 // as above, the inset names can be translated to
583                 // inset codes using insetCode(). And the insets'
584                 // write() routines should use insetName() rather than
585                 // hardcoding it.
586                 if (tmptok == "Quotes") {
587                         inset.reset(new InsetQuotes(buf));
588                 } else if (tmptok == "External") {
589                         inset.reset(new InsetExternal(buf));
590                 } else if (tmptok == "FormulaMacro") {
591                         inset.reset(new MathMacroTemplate(buf));
592                 } else if (tmptok == "Formula") {
593                         inset.reset(new InsetMathHull(buf));
594                 } else if (tmptok == "Graphics") {
595                         inset.reset(new InsetGraphics(buf));
596                 } else if (tmptok == "Note") {
597                         inset.reset(new InsetNote(buf, tmptok));
598                 } else if (tmptok == "Box") {
599                         inset.reset(new InsetBox(buf, tmptok));
600                 } else if (tmptok == "Flex") {
601                         lex.eatLine();
602                         string s = lex.getString();
603                         inset.reset(new InsetFlex(buf, s));
604                 } else if (tmptok == "Branch") {
605                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
606                 } else if (tmptok == "Phantom") {
607                         inset.reset(new InsetPhantom(buf, tmptok));
608                 } else if (tmptok == "ERT") {
609                         inset.reset(new InsetERT(buf));
610                 } else if (tmptok == "listings") {
611                         inset.reset(new InsetListings(buf));
612                 } else if (tmptok == "script") {
613                         inset.reset(new InsetScript(buf));
614                 } else if (tmptok == "space") {
615                         inset.reset(new InsetSpace);
616                 } else if (tmptok == "Tabular") {
617                         inset.reset(new InsetTabular(buf));
618                 } else if (tmptok == "Text") {
619                         inset.reset(new InsetText(buf));
620                 } else if (tmptok == "VSpace") {
621                         inset.reset(new InsetVSpace);
622                 } else if (tmptok == "Foot") {
623                         inset.reset(new InsetFoot(buf));
624                 } else if (tmptok == "Marginal") {
625                         inset.reset(new InsetMarginal(buf));
626                 } else if (tmptok == "Newpage") {
627                         inset.reset(new InsetNewpage);
628                 } else if (tmptok == "Newline") {
629                         inset.reset(new InsetNewline);
630                 } else if (tmptok == "Argument") {
631                         inset.reset(new InsetArgument(buf, tmptok));
632                 } else if (tmptok == "Float") {
633                         inset.reset(new InsetFloat(buf, string()));
634                 } else if (tmptok == "Wrap") {
635                         lex.next();
636                         string tmptok = lex.getString();
637                         inset.reset(new InsetWrap(buf, tmptok));
638                 } else if (tmptok == "Caption") {
639                         lex.eatLine();
640                         string s = lex.getString();
641                         inset.reset(new InsetCaption(buf, s));
642                 } else if (tmptok == "Index") {
643                         inset.reset(new InsetIndex(buf, InsetIndexParams()));
644                 } else if (tmptok == "FloatList") {
645                         inset.reset(new InsetFloatList(buf));
646                 } else if (tmptok == "Info") {
647                         inset.reset(new InsetInfo(buf));
648                 } else if (tmptok == "IPA") {
649                         inset.reset(new InsetIPA(buf));
650                 } else if (tmptok == "IPADeco") {
651                         inset.reset(new InsetIPADeco(buf, tmptok));
652                 } else if (tmptok == "Preview") {
653                         inset.reset(new InsetPreview(buf));
654                 } else {
655                         lyxerr << "unknown Inset type '" << tmptok
656                                << "'" << endl;
657                         while (lex.isOK() && lex.getString() != "\\end_inset")
658                                 lex.next();
659                         return 0;
660                 }
661
662                 // Set the buffer reference for proper parsing of some insets
663                 // (InsetCollapsable for example)
664                 inset->setBuffer(*buf);
665                 inset->read(lex);
666                 // Set again the buffer for insets that are created inside this inset
667                 // (InsetMathHull for example).
668                 inset->setBuffer(*buf);
669         }
670         return inset.release();
671 }
672
673
674 } // namespace lyx