]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
7d344c87d7fae6ab22f48768c654019c7e7cb64c
[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/InsetBox.h"
26 #include "insets/InsetBranch.h"
27 #include "insets/InsetCaption.h"
28 #include "insets/InsetCitation.h"
29 #include "insets/InsetFlex.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/InsetHyperlink.h"
38 #include "insets/InsetInclude.h"
39 #include "insets/InsetIndex.h"
40 #include "insets/InsetInfo.h"
41 #include "insets/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.h"
44 #include "insets/InsetNewline.h"
45 #include "insets/InsetNewpage.h"
46 #include "insets/InsetNomencl.h"
47 #include "insets/InsetNote.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetPhantom.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.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_PHANTOM_INSERT: {
130                         string arg = cmd.getArg(0);
131                         if (arg.empty())
132                                 arg = "Phantom";
133                         return new InsetPhantom(buf, arg);
134                 }
135
136                 case LFUN_ERT_INSERT:
137                         return new InsetERT(buf);
138
139                 case LFUN_LISTING_INSERT:
140                         return new InsetListings(buf);
141
142                 case LFUN_FOOTNOTE_INSERT:
143                         return new InsetFoot(buf);
144
145                 case LFUN_MARGINALNOTE_INSERT:
146                         return new InsetMarginal(buf);
147
148                 case LFUN_OPTIONAL_INSERT:
149                         return new InsetOptArg(buf);
150
151                 case LFUN_FLOAT_INSERT: {
152                         // check if the float type exists
153                         string const type = cmd.getArg(0);
154                         //FIXME: only the float type (the first argument) is transmitted
155                         // because of the InsetFloat ctor.
156                         if (params.documentClass().floats().typeExist(type))
157                                 return new InsetFloat(buf, type);
158                         lyxerr << "Non-existent float type: " << type << endl;
159                         return 0;
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.documentClass().floats().typeExist(argument)) {
166                                 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
167                                 p->setWide(true);
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(buf, argument);
178                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
179                         return 0;
180                 }
181
182                 case LFUN_INDEX_INSERT: {
183                         docstring arg = cmd.argument();
184                         return new InsetIndex(buf, InsetIndexParams(arg));
185                 }
186
187                 case LFUN_NOMENCL_INSERT: {
188                         InsetCommandParams icp(NOMENCL_CODE);
189                         icp["symbol"] = cmd.argument();
190                         return new InsetNomencl(buf, icp);
191                 }
192
193                 case LFUN_TABULAR_INSERT: {
194                         if (cmd.argument().empty())
195                                 return 0;
196                         istringstream ss(to_utf8(cmd.argument()));
197                         int r = 0, c = 0;
198                         ss >> r >> c;
199                         if (r <= 0)
200                                 r = 2;
201                         if (c <= 0)
202                                 c = 2;
203                         return new InsetTabular(buf, r, c);
204                 }
205
206                 case LFUN_CAPTION_INSERT:
207                         return new InsetCaption(buf);
208
209                 case LFUN_INDEX_PRINT:  {
210                         InsetCommandParams icp(INDEX_PRINT_CODE);
211                         icp["type"] = cmd.argument();
212                         return new InsetPrintIndex(buf, icp);
213                 }
214
215                 case LFUN_NOMENCL_PRINT: {
216                         InsetCommandParams icp(NOMENCL_PRINT_CODE);
217                         icp["set_width"] = from_ascii("auto");
218                         return new InsetPrintNomencl(buf, icp);
219                 }
220
221                 case LFUN_TOC_INSERT:
222                         return new InsetTOC(buf, InsetCommandParams(TOC_CODE));
223
224                 case LFUN_INFO_INSERT: {
225                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
226                         inset->updateInfo();
227                         return inset;
228                 }
229
230                 case LFUN_INSET_INSERT: {
231                         string const name = cmd.getArg(0);
232                         InsetCode code = insetCode(name);
233                         switch (code) {
234                         case NO_CODE:
235                                 lyxerr << "No such inset '" << name << "'.";
236                                 return 0;
237                         
238                         case BIBITEM_CODE: {
239                                 InsetCommandParams icp(code);
240                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
241                                 return new InsetBibitem(buf, icp);
242                         }
243                         
244                         case BIBTEX_CODE: {
245                                 InsetCommandParams icp(code);
246                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
247                                 return new InsetBibtex(buf, icp);
248                         }
249                         
250                         case CITE_CODE: {
251                                 InsetCommandParams icp(code);
252                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
253                                 return new InsetCitation(buf, icp);
254                         }
255                         
256                         case ERT_CODE: {
257                                 return new InsetERT(buf,
258                                         InsetERT::string2params(to_utf8(cmd.argument())));
259                         }
260                                 
261                         case LISTINGS_CODE: {
262                                 InsetListingsParams par;
263                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
264                                 return new InsetListings(buf, par);
265                         }
266                         
267                         case EXTERNAL_CODE: {
268                                 InsetExternalParams iep;
269                                 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
270                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
271                                 inset->setBuffer(*buf);
272                                 inset->setParams(iep);
273                                 return inset.release();
274                         }
275                         
276                         case GRAPHICS_CODE: {
277                                 InsetGraphicsParams igp;
278                                 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
279                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
280                                 inset->setParams(igp);
281                                 return inset.release();
282                         }
283                         
284                         case HYPERLINK_CODE: {
285                                 InsetCommandParams icp(code);
286                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
287                                 return new InsetHyperlink(buf, icp);
288                         }
289                         
290                         case INCLUDE_CODE: {
291                                 InsetCommandParams icp(code);
292                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
293                                 return new InsetInclude(buf, icp);
294                         }
295                         
296                         case INDEX_CODE: {
297                                 docstring arg = cmd.argument();
298                                 return new InsetIndex(buf, InsetIndexParams(arg));
299                         }
300                         
301                         case INDEX_PRINT_CODE:  {
302                                 InsetCommandParams icp(code);
303                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
304                                 return new InsetPrintIndex(buf, icp);
305                         }
306                         
307                         case NOMENCL_CODE: {
308                                 InsetCommandParams icp(code);
309                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
310                                 return new InsetNomencl(buf, icp);
311                         }
312                         
313                         case LABEL_CODE: {
314                                 InsetCommandParams icp(code);
315                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
316                                 return new InsetLabel(buf, icp);
317                         }
318                         
319                         case REF_CODE: {
320                                 InsetCommandParams icp(code);
321                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
322                                 return new InsetRef(buf, icp);
323                         }
324
325                         case SPACE_CODE: {
326                                 InsetSpaceParams isp;
327                                 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
328                                 return new InsetSpace(isp);
329                         }
330                         
331                         case TOC_CODE: {
332                                 InsetCommandParams icp(code);
333                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
334                                 return new InsetTOC(buf, icp);
335                         }
336                         
337                         case VSPACE_CODE: {
338                                 VSpace vspace;
339                                 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
340                                 return new InsetVSpace(vspace);
341                         }
342                         
343                         default:
344                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
345                                                 << endl;
346                                 return 0;
347                         
348                         }
349                 } //end LFUN_INSET_INSERT
350
351                 case LFUN_SPACE_INSERT: {
352                         string const name = cmd.getArg(0);
353                         string const len = cmd.getArg(1);
354                         if (name.empty()) {
355                                 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
356                                 break;
357                         }
358                         InsetSpaceParams isp;
359                         // The tests for isp.math might be disabled after a file format change
360                         if (name == "normal")
361                                 isp.kind = InsetSpaceParams::NORMAL;
362                         else if (name == "protected")
363                                 isp.kind = InsetSpaceParams::PROTECTED;
364                         else if (name == "thin")
365                                 isp.kind = InsetSpaceParams::THIN;
366                         else if (isp.math && name == "med")
367                                 isp.kind = InsetSpaceParams::MEDIUM;
368                         else if (isp.math && name == "thick")
369                                 isp.kind = InsetSpaceParams::THICK;
370                         else if (name == "quad")
371                                 isp.kind = InsetSpaceParams::QUAD;
372                         else if (name == "qquad")
373                                 isp.kind = InsetSpaceParams::QQUAD;
374                         else if (name == "enspace")
375                                 isp.kind = InsetSpaceParams::ENSPACE;
376                         else if (name == "enskip")
377                                 isp.kind = InsetSpaceParams::ENSKIP;
378                         else if (name == "negthinspace")
379                                 isp.kind = InsetSpaceParams::NEGTHIN;
380                         else if (isp.math && name == "negmedspace")
381                                 isp.kind = InsetSpaceParams::NEGMEDIUM;
382                         else if (isp.math && name == "negthickspace")
383                                 isp.kind = InsetSpaceParams::NEGTHICK;
384                         else if (name == "hfill")
385                                 isp.kind = InsetSpaceParams::HFILL;
386                         else if (name == "hfill*")
387                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
388                         else if (name == "dotfill")
389                                 isp.kind = InsetSpaceParams::DOTFILL;
390                         else if (name == "hrulefill")
391                                 isp.kind = InsetSpaceParams::HRULEFILL;
392                         else if (name == "hspace") {
393                                 if (len.empty() || !isValidGlueLength(len)) {
394                                         lyxerr << "LyX function 'space-insert hspace' "
395                                                << "needs a valid length argument." << endl;
396                                         break;
397                                 }
398                                 isp.kind = InsetSpaceParams::CUSTOM;
399                                 isp.length = GlueLength(len);
400                         }
401                         else if (name == "hspace*") {
402                                 if (len.empty() || !isValidGlueLength(len)) {
403                                         lyxerr << "LyX function 'space-insert hspace*' "
404                                                << "needs a valid length argument." << endl;
405                                         break;
406                                 }
407                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
408                                 isp.length = GlueLength(len);
409                         }
410                         else {
411                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
412                                 break;
413                         }
414                         return new InsetSpace(isp);
415                 }
416                 break;
417
418                 default:
419                         break;
420                 }
421
422         } catch (ExceptionMessage const & message) {
423                 if (message.type_ == ErrorException) {
424                         // This should never happen!
425                         Alert::error(message.title_, message.details_);
426                         lyx_exit(1);
427                 } else if (message.type_ == WarningException) {
428                         Alert::warning(message.title_, message.details_);
429                         return 0;
430                 }
431         }
432
433         return 0;
434 }
435
436
437 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
438 {
439         Inset * inset = createInsetHelper(buf, cmd);
440         if (inset)
441                 inset->setBuffer(*buf);
442         return inset;
443 }
444
445
446 Inset * readInset(Lexer & lex, Buffer * buf)
447 {
448         // consistency check
449         if (lex.getString() != "\\begin_inset")
450                 LYXERR0("Buffer::readInset: Consistency check failed.");
451
452         auto_ptr<Inset> inset;
453
454         string tmptok;
455         lex >> tmptok;
456
457         // test the different insets
458         
459         // FIXME It would be better if we did not have this branch and could
460         // just do one massive switch for all insets. But at present, it's
461         // easier to do it this way, and we can't do the massive switch until
462         // the conversion mentioned below.  Note that if we do want to do a
463         // single switch, we need to remove this "CommandInset" line---or
464         // replace it with a single "InsetType" line that would be used in all
465         // insets.
466         if (tmptok == "CommandInset") {
467                 lex.next();
468                 string const insetType = lex.getString();
469                 lex.pushToken(insetType);
470                 
471                 InsetCode const code = insetCode(insetType);
472                 
473                 //FIXME If we do the one massive switch, we cannot do this here, since
474                 //we do not know in advance that we're dealing with a command inset.
475                 //Worst case, we could put it in each case below. Better, we could
476                 //pass the lexer to the constructor and let the params be built there.
477                 InsetCommandParams inscmd(code);
478                 inscmd.read(lex);
479
480                 switch (code) {
481                         case BIBITEM_CODE:
482                                 inset.reset(new InsetBibitem(buf, inscmd));
483                                 break;
484                         case BIBTEX_CODE:
485                                 inset.reset(new InsetBibtex(buf, inscmd));
486                                 break;
487                         case CITE_CODE: 
488                                 inset.reset(new InsetCitation(buf, inscmd));
489                                 break;
490                         case HYPERLINK_CODE:
491                                 inset.reset(new InsetHyperlink(buf, inscmd));
492                                 break;
493                         case INCLUDE_CODE:
494                                 inset.reset(new InsetInclude(buf, inscmd));
495                                 break;
496                         case INDEX_PRINT_CODE:
497                                 inset.reset(new InsetPrintIndex(buf, inscmd));
498                                 break;
499                         case LABEL_CODE:
500                                 inset.reset(new InsetLabel(buf, inscmd));
501                                 break;
502                         case NOMENCL_CODE:
503                                 inset.reset(new InsetNomencl(buf, inscmd));
504                                 break;
505                         case NOMENCL_PRINT_CODE:
506                                 inset.reset(new InsetPrintNomencl(buf, inscmd));
507                                 break;
508                         case REF_CODE:
509                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
510                                         return 0;
511                                 inset.reset(new InsetRef(buf, inscmd));
512                                 break;
513                         case TOC_CODE:
514                                 inset.reset(new InsetTOC(buf, inscmd));
515                                 break;
516                         case NO_CODE:
517                         default:
518                                 lyxerr << "unknown CommandInset '" << insetType
519                                                         << "'" << endl;
520                                 while (lex.isOK() && lex.getString() != "\\end_inset")
521                                         lex.next();
522                                 return 0;
523                 }
524                 inset->setBuffer(*buf);
525         } else { 
526                 // FIXME This branch should be made to use inset codes as the preceding 
527                 // branch does. Unfortunately, that will take some doing. It requires
528                 // converting the representation of the insets in LyX files so that they
529                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
530                 // can be translated to inset codes using insetCode(). And the insets'
531                 // write() routines should use insetName() rather than hardcoding it.
532                 if (tmptok == "Quotes") {
533                         inset.reset(new InsetQuotes(buf));
534                 } else if (tmptok == "External") {
535                         inset.reset(new InsetExternal(buf));
536                 } else if (tmptok == "FormulaMacro") {
537                         inset.reset(new MathMacroTemplate(buf));
538                 } else if (tmptok == "Formula") {
539                         inset.reset(new InsetMathHull(buf));
540                 } else if (tmptok == "Graphics") {
541                         inset.reset(new InsetGraphics(buf));
542                 } else if (tmptok == "Note") {
543                         inset.reset(new InsetNote(buf, tmptok));
544                 } else if (tmptok == "Box") {
545                         inset.reset(new InsetBox(buf, tmptok));
546                 } else if (tmptok == "Flex") {
547                         lex.eatLine();
548                         string s = lex.getString();
549                         inset.reset(new InsetFlex(buf, s));
550                 } else if (tmptok == "Branch") {
551                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
552                 } else if (tmptok == "Phantom") {
553                         inset.reset(new InsetPhantom(buf, tmptok));
554                 } else if (tmptok == "ERT") {
555                         inset.reset(new InsetERT(buf));
556                 } else if (tmptok == "listings") {
557                         inset.reset(new InsetListings(buf));
558                 } else if (tmptok == "space") {
559                         inset.reset(new InsetSpace);
560                 } else if (tmptok == "Tabular") {
561                         inset.reset(new InsetTabular(buf));
562                 } else if (tmptok == "Text") {
563                         inset.reset(new InsetText(buf));
564                 } else if (tmptok == "VSpace") {
565                         inset.reset(new InsetVSpace);
566                 } else if (tmptok == "Foot") {
567                         inset.reset(new InsetFoot(buf));
568                 } else if (tmptok == "Marginal") {
569                         inset.reset(new InsetMarginal(buf));
570                 } else if (tmptok == "Newpage") {
571                         inset.reset(new InsetNewpage);
572                 } else if (tmptok == "Newline") {
573                         inset.reset(new InsetNewline);
574                 } else if (tmptok == "OptArg") {
575                         inset.reset(new InsetOptArg(buf));
576                 } else if (tmptok == "Float") {
577                         lex.next();
578                         string tmptok = lex.getString();
579                         inset.reset(new InsetFloat(buf, tmptok));
580                 } else if (tmptok == "Wrap") {
581                         lex.next();
582                         string tmptok = lex.getString();
583                         inset.reset(new InsetWrap(buf, tmptok));
584                 } else if (tmptok == "Caption") {
585                         inset.reset(new InsetCaption(buf));
586                 } else if (tmptok == "Index") {
587                         inset.reset(new InsetIndex(buf, InsetIndexParams()));
588                 } else if (tmptok == "FloatList") {
589                         inset.reset(new InsetFloatList(buf));
590                 } else if (tmptok == "Info") {
591                         inset.reset(new InsetInfo(buf));
592                 } else {
593                         lyxerr << "unknown Inset type '" << tmptok
594                                << "'" << endl;
595                         while (lex.isOK() && lex.getString() != "\\end_inset")
596                                 lex.next();
597                         return 0;
598                 }
599
600                 // Set the buffer reference for proper parsing of some insets
601                 // (InsetCollapsable for example)
602                 inset->setBuffer(*buf);
603                 inset->read(lex);
604                 // Set again the buffer for insets that are created inside this inset
605                 // (InsetMathHull for example).
606                 inset->setBuffer(*buf);
607         }
608         return inset.release();
609 }
610
611
612 } // namespace lyx