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