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