]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
Thanks, Abdel.
[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/InsetERT.h"
29 #include "insets/InsetListings.h"
30 #include "insets/InsetExternal.h"
31 #include "insets/InsetFloat.h"
32 #include "insets/InsetFloatList.h"
33 #include "insets/InsetFoot.h"
34 #include "insets/InsetGraphics.h"
35 #include "insets/InsetInclude.h"
36 #include "insets/InsetIndex.h"
37 #include "insets/InsetInfo.h"
38 #include "insets/InsetNomencl.h"
39 #include "insets/InsetLabel.h"
40 #include "insets/InsetLine.h"
41 #include "insets/InsetMarginal.h"
42 #include "insets/InsetNewline.h"
43 #include "insets/InsetNewpage.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 "support/lassert.h"
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
86                 case LFUN_LINE_INSERT:
87                         return new InsetLine;
88
89                 case LFUN_NEWPAGE_INSERT: {
90                         string const name = cmd.getArg(0);
91                         InsetNewpageParams inp;
92                         if (name.empty() || name == "newpage")
93                                 inp.kind = InsetNewpageParams::NEWPAGE;
94                         else if (name == "pagebreak")
95                                 inp.kind = InsetNewpageParams::PAGEBREAK;
96                         else if (name == "clearpage")
97                                 inp.kind = InsetNewpageParams::CLEARPAGE;
98                         else if (name == "cleardoublepage")
99                                 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
100                         return new InsetNewpage(inp);
101                 }
102
103                 case LFUN_FLEX_INSERT: {
104                         string s = cmd.getArg(0);
105                         return new InsetFlex(buf, 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(buf, 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->setWide(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_INFO_INSERT: {
210                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
211                         inset->updateInfo();
212                         return inset;
213                 }
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                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
226                                 return new InsetBibitem(buf, icp);
227                         }
228                         
229                         case BIBTEX_CODE: {
230                                 InsetCommandParams icp(code);
231                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
232                                 return new InsetBibtex(buf, icp);
233                         }
234                         
235                         case CITE_CODE: {
236                                 InsetCommandParams icp(code);
237                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
238                                 return new InsetCitation(icp);
239                         }
240                         
241                         case ERT_CODE: {
242                                 return new InsetERT(buf,
243                                         InsetERT::string2params(to_utf8(cmd.argument())));
244                         }
245                                 
246                         case LISTINGS_CODE: {
247                                 InsetListingsParams par;
248                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
249                                 return new InsetListings(buf, par);
250                         }
251                         
252                         case EXTERNAL_CODE: {
253                                 InsetExternalParams iep;
254                                 InsetExternal::string2params(to_utf8(cmd.argument()), buf, iep);
255                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
256                                 inset->setBuffer(buf);
257                                 inset->setParams(iep);
258                                 return inset.release();
259                         }
260                         
261                         case GRAPHICS_CODE: {
262                                 InsetGraphicsParams igp;
263                                 InsetGraphics::string2params(to_utf8(cmd.argument()), buf, igp);
264                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
265                                 inset->setParams(igp);
266                                 return inset.release();
267                         }
268                         
269                         case HYPERLINK_CODE: {
270                                 InsetCommandParams icp(code);
271                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
272                                 return new InsetHyperlink(icp);
273                         }
274                         
275                         case INCLUDE_CODE: {
276                                 InsetCommandParams icp(code);
277                                 InsetCommand::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                                 InsetCommand::string2params(name, lyx::to_utf8(cmd.argument()), icp);
287                                 return new InsetNomencl(icp);
288                         }
289                         
290                         case LABEL_CODE: {
291                                 InsetCommandParams icp(code);
292                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
293                                 return new InsetLabel(icp);
294                         }
295                         
296                         case REF_CODE: {
297                                 InsetCommandParams icp(code);
298                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
299                                 return new InsetRef(buf, icp);
300                         }
301
302                         case SPACE_CODE: {
303                                 InsetSpaceParams isp;
304                                 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
305                                 return new InsetSpace(isp);
306                         }
307                         
308                         case TOC_CODE: {
309                                 InsetCommandParams icp(code);
310                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
311                                 return new InsetTOC(icp);
312                         }
313                         
314                         case VSPACE_CODE: {
315                                 VSpace vspace;
316                                 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
317                                 return new InsetVSpace(vspace);
318                         }
319                         
320                         default:
321                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
322                                                 << endl;
323                                 return 0;
324                         
325                         }
326                         } //end LFUN_INSET_INSERT
327
328                 case LFUN_SPACE_INSERT: {
329                         string const name = cmd.getArg(0);
330                         string const len = cmd.getArg(1);
331                         if (name.empty()) {
332                                 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
333                                 break;
334                         }
335                         InsetSpaceParams isp;
336                         if (name == "normal")
337                                 isp.kind = InsetSpaceParams::NORMAL;
338                         else if (name == "protected")
339                                 isp.kind = InsetSpaceParams::PROTECTED;
340                         else if (name == "thin")
341                                 isp.kind = InsetSpaceParams::THIN;
342                         else if (name == "quad")
343                                 isp.kind = InsetSpaceParams::QUAD;
344                         else if (name == "qquad")
345                                 isp.kind = InsetSpaceParams::QQUAD;
346                         else if (name == "enspace")
347                                 isp.kind = InsetSpaceParams::ENSPACE;
348                         else if (name == "enskip")
349                                 isp.kind = InsetSpaceParams::ENSKIP;
350                         else if (name == "negthinspace")
351                                 isp.kind = InsetSpaceParams::NEGTHIN;
352                         else if (name == "hfill")
353                                 isp.kind = InsetSpaceParams::HFILL;
354                         else if (name == "hfill*")
355                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
356                         else if (name == "dotfill")
357                                 isp.kind = InsetSpaceParams::DOTFILL;
358                         else if (name == "hrulefill")
359                                 isp.kind = InsetSpaceParams::HRULEFILL;
360                         else if (name == "hspace") {
361                                 if (len.empty() || !isValidLength(len)) {
362                                         lyxerr << "LyX function 'space-insert hspace' "
363                                                << "needs a valid length argument." << endl;
364                                         break;
365                                 }
366                                 isp.kind = InsetSpaceParams::CUSTOM;
367                                 isp.length = Length(len);
368                         }
369                         else if (name == "hspace*") {
370                                 if (len.empty() || !isValidLength(len)) {
371                                         lyxerr << "LyX function 'space-insert hspace*' "
372                                                << "needs a valid length argument." << endl;
373                                         break;
374                                 }
375                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
376                                 isp.length = Length(len);
377                         }
378                         else {
379                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
380                                 break;
381                         }
382                         return new InsetSpace(isp);
383                 }
384                 break;
385
386                 default:
387                         break;
388                 }
389
390         } catch (ExceptionMessage const & message) {
391                 if (message.type_ == ErrorException) {
392                         // This should never happen!
393                         Alert::error(message.title_, message.details_);
394                         LyX::cref().exit(1);
395                 } else if (message.type_ == WarningException) {
396                         Alert::warning(message.title_, message.details_);
397                         return 0;
398                 }
399         }
400
401         return 0;
402 }
403
404
405 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
406 {
407         Inset * inset = createInsetHelper(buf, cmd);
408         if (inset)
409                 inset->setBuffer(buf);
410         return inset;
411 }
412
413
414 Inset * readInset(Lexer & lex, Buffer const & buf)
415 {
416         // consistency check
417         if (lex.getString() != "\\begin_inset")
418                 LYXERR0("Buffer::readInset: Consistency check failed.");
419
420         auto_ptr<Inset> inset;
421
422         string tmptok;
423         lex >> tmptok;
424
425         // test the different insets
426         
427         // FIXME It would be better if we did not have this branch and could
428         // just do one massive switch for all insets. But at present, it's
429         // easier to do it this way, and we can't do the massive switch until
430         // the conversion mentioned below.  Note that if we do want to do a
431         // single switch, we need to remove this "CommandInset" line---or
432         // replace it with a single "InsetType" line that would be used in all
433         // insets.
434         if (tmptok == "CommandInset") {
435                 lex.next();
436                 string const insetType = lex.getString();
437                 lex.pushToken(insetType);
438                 
439                 InsetCode const code = insetCode(insetType);
440                 
441                 //FIXME If we do the one massive switch, we cannot do this here, since
442                 //we do not know in advance that we're dealing with a command inset.
443                 //Worst case, we could put it in each case below. Better, we could
444                 //pass the lexer to the constructor and let the params be built there.
445                 InsetCommandParams inscmd(code);
446                 inscmd.read(lex);
447
448                 switch (code) {
449                         case BIBITEM_CODE:
450                                 inset.reset(new InsetBibitem(buf, inscmd));
451                                 break;
452                         case BIBTEX_CODE:
453                                 inset.reset(new InsetBibtex(buf, inscmd));
454                                 break;
455                         case CITE_CODE: 
456                                 inset.reset(new InsetCitation(inscmd));
457                                 break;
458                         case HYPERLINK_CODE:
459                                 inset.reset(new InsetHyperlink(inscmd));
460                                 break;
461                         case INCLUDE_CODE:
462                                 inset.reset(new InsetInclude(inscmd));
463                                 break;
464                         case INDEX_PRINT_CODE:
465                                 inset.reset(new InsetPrintIndex(inscmd));
466                                 break;
467                         case LABEL_CODE:
468                                 inset.reset(new InsetLabel(inscmd));
469                                 break;
470                         case NOMENCL_CODE:
471                                 inset.reset(new InsetNomencl(inscmd));
472                                 break;
473                         case NOMENCL_PRINT_CODE:
474                                 inset.reset(new InsetPrintNomencl(inscmd));
475                                 break;
476                         case REF_CODE:
477                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
478                                         return 0;
479                                 inset.reset(new InsetRef(buf, inscmd));
480                                 break;
481                         case TOC_CODE:
482                                 inset.reset(new InsetTOC(inscmd));
483                                 break;
484                         case NO_CODE:
485                         default:
486                                 lyxerr << "unknown CommandInset '" << insetType
487                                                         << "'" << endl;
488                                 while (lex.isOK() && lex.getString() != "\\end_inset")
489                                         lex.next();
490                                 return 0;
491                 }
492                 inset->setBuffer(const_cast<Buffer &>(buf));
493         } else { 
494                 // FIXME This branch should be made to use inset codes as the preceding 
495                 // branch does. Unfortunately, that will take some doing. It requires
496                 // converting the representation of the insets in LyX files so that they
497                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
498                 // can be translated to inset codes using insetCode(). And the insets'
499                 // write() routines should use insetName() rather than hardcoding it.
500                 if (tmptok == "Quotes") {
501                         inset.reset(new InsetQuotes);
502                 } else if (tmptok == "External") {
503                         inset.reset(new InsetExternal(const_cast<Buffer &>(buf)));
504                 } else if (tmptok == "FormulaMacro") {
505                         inset.reset(new MathMacroTemplate);
506                 } else if (tmptok == "Formula") {
507                         inset.reset(new InsetMathHull);
508                 } else if (tmptok == "Graphics") {
509                         inset.reset(new InsetGraphics(const_cast<Buffer &>(buf)));
510                 } else if (tmptok == "Note") {
511                         inset.reset(new InsetNote(buf, tmptok));
512                 } else if (tmptok == "Box") {
513                         inset.reset(new InsetBox(buf, tmptok));
514                 } else if (tmptok == "Flex") {
515                         lex.next();
516                         string s = lex.getString();
517                         inset.reset(new InsetFlex(buf, s));
518                 } else if (tmptok == "Branch") {
519                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
520                 } else if (tmptok == "ERT") {
521                         inset.reset(new InsetERT(buf));
522                 } else if (tmptok == "listings") {
523                         inset.reset(new InsetListings(buf));
524                 } else if (tmptok == "space") {
525                         inset.reset(new InsetSpace);
526                 } else if (tmptok == "Tabular") {
527                         inset.reset(new InsetTabular(buf));
528                 } else if (tmptok == "Text") {
529                         inset.reset(new InsetText(buf));
530                 } else if (tmptok == "VSpace") {
531                         inset.reset(new InsetVSpace);
532                 } else if (tmptok == "Foot") {
533                         inset.reset(new InsetFoot(buf));
534                 } else if (tmptok == "Marginal") {
535                         inset.reset(new InsetMarginal(buf));
536                 } else if (tmptok == "Newpage") {
537                         inset.reset(new InsetNewpage);
538                 } else if (tmptok == "Newline") {
539                         inset.reset(new InsetNewline);
540                 } else if (tmptok == "OptArg") {
541                         inset.reset(new InsetOptArg(buf));
542                 } else if (tmptok == "Float") {
543                         lex.next();
544                         string tmptok = lex.getString();
545                         inset.reset(new InsetFloat(buf, tmptok));
546                 } else if (tmptok == "Wrap") {
547                         lex.next();
548                         string tmptok = lex.getString();
549                         inset.reset(new InsetWrap(buf, tmptok));
550                 } else if (tmptok == "Caption") {
551                         inset.reset(new InsetCaption(buf));
552                 } else if (tmptok == "Index") {
553                         inset.reset(new InsetIndex(buf));
554                 } else if (tmptok == "FloatList") {
555                         inset.reset(new InsetFloatList);
556                 } else if (tmptok == "Info") {
557                         inset.reset(new InsetInfo(buf));
558                 } else {
559                         lyxerr << "unknown Inset type '" << tmptok
560                                << "'" << endl;
561                         while (lex.isOK() && lex.getString() != "\\end_inset")
562                                 lex.next();
563                         return 0;
564                 }
565
566                 // Set the buffer reference for proper parsing of some insets
567                 // (InsetCollapsable for example)
568                 inset->setBuffer(const_cast<Buffer &>(buf));
569                 inset->read(lex);
570                 // Set again the buffer for insets that are created inside this inset
571                 // (InsetMathHull for example).
572                 inset->setBuffer(const_cast<Buffer &>(buf));
573         }
574         return inset.release();
575 }
576
577
578 } // namespace lyx