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