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