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