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