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