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