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