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